0067-generic-Mangle-bootloader-s-kernel-arguments.patch 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. From 71270226b14733a4b1f2cde58ea9265caa50b38d Mon Sep 17 00:00:00 2001
  2. From: Adrian Panella <[email protected]>
  3. Date: Thu, 9 Mar 2017 09:37:17 +0100
  4. Subject: [PATCH 67/69] generic: Mangle bootloader's kernel arguments
  5. The command-line arguments provided by the boot loader will be
  6. appended to a new device tree property: bootloader-args.
  7. If there is a property "append-rootblock" in DT under /chosen
  8. and a root= option in bootloaders command line it will be parsed
  9. and added to DT bootargs with the form: <append-rootblock>XX.
  10. Only command line ATAG will be processed, the rest of the ATAGs
  11. sent by bootloader will be ignored.
  12. This is usefull in dual boot systems, to get the current root partition
  13. without afecting the rest of the system.
  14. Signed-off-by: Adrian Panella <[email protected]>
  15. ---
  16. arch/arm/Kconfig | 11 +++++
  17. arch/arm/boot/compressed/atags_to_fdt.c | 72 ++++++++++++++++++++++++++++++++-
  18. init/main.c | 16 ++++++++
  19. 3 files changed, 98 insertions(+), 1 deletion(-)
  20. --- a/arch/arm/Kconfig
  21. +++ b/arch/arm/Kconfig
  22. @@ -1588,6 +1588,17 @@ config ARM_ATAG_DTB_COMPAT_CMDLINE_EXTEN
  23. The command-line arguments provided by the boot loader will be
  24. appended to the the device tree bootargs property.
  25. +config ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
  26. + bool "Append rootblock parsing bootloader's kernel arguments"
  27. + help
  28. + The command-line arguments provided by the boot loader will be
  29. + appended to a new device tree property: bootloader-args.
  30. + If there is a property "append-rootblock" in DT under /chosen
  31. + and a root= option in bootloaders command line it will be parsed
  32. + and added to DT bootargs with the form: <append-rootblock>XX.
  33. + Only command line ATAG will be processed, the rest of the ATAGs
  34. + sent by bootloader will be ignored.
  35. +
  36. endchoice
  37. config CMDLINE
  38. --- a/arch/arm/boot/compressed/atags_to_fdt.c
  39. +++ b/arch/arm/boot/compressed/atags_to_fdt.c
  40. @@ -5,6 +5,8 @@
  41. #if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_EXTEND)
  42. #define do_extend_cmdline 1
  43. +#elif defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
  44. +#define do_extend_cmdline 1
  45. #else
  46. #define do_extend_cmdline 0
  47. #endif
  48. @@ -20,6 +22,7 @@ static int node_offset(void *fdt, const
  49. return offset;
  50. }
  51. +#ifndef CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
  52. static int setprop(void *fdt, const char *node_path, const char *property,
  53. void *val_array, int size)
  54. {
  55. @@ -28,6 +31,7 @@ static int setprop(void *fdt, const char
  56. return offset;
  57. return fdt_setprop(fdt, offset, property, val_array, size);
  58. }
  59. +#endif
  60. static int setprop_string(void *fdt, const char *node_path,
  61. const char *property, const char *string)
  62. @@ -38,6 +42,7 @@ static int setprop_string(void *fdt, con
  63. return fdt_setprop_string(fdt, offset, property, string);
  64. }
  65. +#ifndef CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
  66. static int setprop_cell(void *fdt, const char *node_path,
  67. const char *property, uint32_t val)
  68. {
  69. @@ -46,6 +51,7 @@ static int setprop_cell(void *fdt, const
  70. return offset;
  71. return fdt_setprop_cell(fdt, offset, property, val);
  72. }
  73. +#endif
  74. static const void *getprop(const void *fdt, const char *node_path,
  75. const char *property, int *len)
  76. @@ -58,6 +64,7 @@ static const void *getprop(const void *f
  77. return fdt_getprop(fdt, offset, property, len);
  78. }
  79. +#ifndef CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
  80. static uint32_t get_cell_size(const void *fdt)
  81. {
  82. int len;
  83. @@ -68,6 +75,81 @@ static uint32_t get_cell_size(const void
  84. cell_size = fdt32_to_cpu(*size_len);
  85. return cell_size;
  86. }
  87. +#endif
  88. +
  89. +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
  90. +/**
  91. + * taken from arch/x86/boot/string.c
  92. + * local_strstr - Find the first substring in a %NUL terminated string
  93. + * @s1: The string to be searched
  94. + * @s2: The string to search for
  95. + */
  96. +static char *local_strstr(const char *s1, const char *s2)
  97. +{
  98. + size_t l1, l2;
  99. +
  100. + l2 = strlen(s2);
  101. + if (!l2)
  102. + return (char *)s1;
  103. + l1 = strlen(s1);
  104. + while (l1 >= l2) {
  105. + l1--;
  106. + if (!memcmp(s1, s2, l2))
  107. + return (char *)s1;
  108. + s1++;
  109. + }
  110. + return NULL;
  111. +}
  112. +
  113. +static char *append_rootblock(char *dest, const char *str, int len, void *fdt)
  114. +{
  115. + char *ptr, *end, *tmp;
  116. + const char *root="root=";
  117. + const char *find_rootblock;
  118. + int i, l;
  119. + const char *rootblock;
  120. +
  121. + find_rootblock = getprop(fdt, "/chosen", "find-rootblock", &l);
  122. + if (!find_rootblock)
  123. + find_rootblock = root;
  124. +
  125. + //ARM doesn't have __HAVE_ARCH_STRSTR, so it was copied from x86
  126. + ptr = local_strstr(str, find_rootblock);
  127. +
  128. + if(!ptr)
  129. + return dest;
  130. +
  131. + end = strchr(ptr, ' ');
  132. + end = end ? (end - 1) : (strchr(ptr, 0) - 1);
  133. +
  134. + // Some boards ubi.mtd=XX,ZZZZ, so let's check for '," too.
  135. + tmp = strchr(ptr, ',');
  136. +
  137. + if(tmp)
  138. + end = end < tmp ? end : tmp - 1;
  139. +
  140. + //find partition number (assumes format root=/dev/mtdXX | /dev/mtdblockXX | yy:XX | ubi.mtd=XX,ZZZZ )
  141. + for( i = 0; end >= ptr && *end >= '0' && *end <= '9'; end--, i++);
  142. + ptr = end + 1;
  143. +
  144. + /* if append-rootblock property is set use it to append to command line */
  145. + rootblock = getprop(fdt, "/chosen", "append-rootblock", &l);
  146. + if(rootblock != NULL) {
  147. + if(*dest != ' ') {
  148. + *dest = ' ';
  149. + dest++;
  150. + len++;
  151. + }
  152. + if (len + l + i <= COMMAND_LINE_SIZE) {
  153. + memcpy(dest, rootblock, l);
  154. + dest += l - 1;
  155. + memcpy(dest, ptr, i);
  156. + dest += i;
  157. + }
  158. + }
  159. + return dest;
  160. +}
  161. +#endif
  162. static void merge_fdt_bootargs(void *fdt, const char *fdt_cmdline)
  163. {
  164. @@ -88,18 +170,28 @@ static void merge_fdt_bootargs(void *fdt
  165. /* and append the ATAG_CMDLINE */
  166. if (fdt_cmdline) {
  167. +
  168. +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
  169. + //save original bootloader args
  170. + //and append ubi.mtd with root partition number to current cmdline
  171. + setprop_string(fdt, "/chosen", "bootloader-args", fdt_cmdline);
  172. + ptr = append_rootblock(ptr, fdt_cmdline, len, fdt);
  173. +
  174. +#else
  175. len = strlen(fdt_cmdline);
  176. if (ptr - cmdline + len + 2 < COMMAND_LINE_SIZE) {
  177. *ptr++ = ' ';
  178. memcpy(ptr, fdt_cmdline, len);
  179. ptr += len;
  180. }
  181. +#endif
  182. }
  183. *ptr = '\0';
  184. setprop_string(fdt, "/chosen", "bootargs", cmdline);
  185. }
  186. +#ifndef CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
  187. static void hex_str(char *out, uint32_t value)
  188. {
  189. uint32_t digit;
  190. @@ -117,6 +209,7 @@ static void hex_str(char *out, uint32_t
  191. }
  192. *out = '\0';
  193. }
  194. +#endif
  195. /*
  196. * Convert and fold provided ATAGs into the provided FDT.
  197. @@ -131,9 +224,11 @@ int atags_to_fdt(void *atag_list, void *
  198. struct tag *atag = atag_list;
  199. /* In the case of 64 bits memory size, need to reserve 2 cells for
  200. * address and size for each bank */
  201. +#ifndef CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
  202. __be32 mem_reg_property[2 * 2 * NR_BANKS];
  203. - int memcount = 0;
  204. - int ret, memsize;
  205. + int memsize, memcount = 0;
  206. +#endif
  207. + int ret;
  208. /* make sure we've got an aligned pointer */
  209. if ((u32)atag_list & 0x3)
  210. @@ -168,7 +263,9 @@ int atags_to_fdt(void *atag_list, void *
  211. else
  212. setprop_string(fdt, "/chosen", "bootargs",
  213. atag->u.cmdline.cmdline);
  214. - } else if (atag->hdr.tag == ATAG_MEM) {
  215. + }
  216. +#ifndef CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
  217. + else if (atag->hdr.tag == ATAG_MEM) {
  218. if (memcount >= sizeof(mem_reg_property)/4)
  219. continue;
  220. if (!atag->u.mem.size)
  221. @@ -212,6 +309,10 @@ int atags_to_fdt(void *atag_list, void *
  222. setprop(fdt, "/memory", "reg", mem_reg_property,
  223. 4 * memcount * memsize);
  224. }
  225. +#else
  226. +
  227. + }
  228. +#endif
  229. return fdt_pack(fdt);
  230. }
  231. --- a/init/main.c
  232. +++ b/init/main.c
  233. @@ -112,6 +112,10 @@
  234. #include <kunit/test.h>
  235. +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
  236. +#include <linux/of.h>
  237. +#endif
  238. +
  239. static int kernel_init(void *);
  240. extern void init_IRQ(void);
  241. @@ -995,6 +999,18 @@ asmlinkage __visible void __init __no_sa
  242. pr_notice("Kernel command line: %s\n", saved_command_line);
  243. /* parameters may set static keys */
  244. jump_label_init();
  245. +
  246. +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
  247. + //Show bootloader's original command line for reference
  248. + if(of_chosen) {
  249. + const char *prop = of_get_property(of_chosen, "bootloader-args", NULL);
  250. + if(prop)
  251. + pr_notice("Bootloader command line (ignored): %s\n", prop);
  252. + else
  253. + pr_notice("Bootloader command line not present\n");
  254. + }
  255. +#endif
  256. +
  257. parse_early_param();
  258. after_dashes = parse_args("Booting kernel",
  259. static_command_line, __start___param,