996-generic-Mangle-bootloader-s-kernel-arguments.patch 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. @@ -1727,6 +1727,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. @@ -69,6 +76,61 @@ static uint32_t get_cell_size(const void
  84. return cell_size;
  85. }
  86. +#endif
  87. +
  88. +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
  89. +
  90. +static char *append_rootblock(char *dest, const char *str, int len, void *fdt)
  91. +{
  92. + const char *ptr, *end;
  93. + const char *root="root=";
  94. + int i, l;
  95. + const char *rootblock;
  96. +
  97. + //ARM doesn't have __HAVE_ARCH_STRSTR, so search manually
  98. + ptr = str - 1;
  99. +
  100. + do {
  101. + //first find an 'r' at the begining or after a space
  102. + do {
  103. + ptr++;
  104. + ptr = strchr(ptr, 'r');
  105. + if(!ptr) return dest;
  106. +
  107. + } while (ptr != str && *(ptr-1) != ' ');
  108. +
  109. + //then check for the rest
  110. + for(i = 1; i <= 4; i++)
  111. + if(*(ptr+i) != *(root+i)) break;
  112. +
  113. + } while (i != 5);
  114. +
  115. + end = strchr(ptr, ' ');
  116. + end = end ? (end - 1) : (strchr(ptr, 0) - 1);
  117. +
  118. + //find partition number (assumes format root=/dev/mtdXX | /dev/mtdblockXX | yy:XX )
  119. + for( i = 0; end >= ptr && *end >= '0' && *end <= '9'; end--, i++);
  120. + ptr = end + 1;
  121. +
  122. + /* if append-rootblock property is set use it to append to command line */
  123. + rootblock = getprop(fdt, "/chosen", "append-rootblock", &l);
  124. + if(rootblock != NULL) {
  125. + if(*dest != ' ') {
  126. + *dest = ' ';
  127. + dest++;
  128. + len++;
  129. + }
  130. + if (len + l + i <= COMMAND_LINE_SIZE) {
  131. + memcpy(dest, rootblock, l);
  132. + dest += l - 1;
  133. + memcpy(dest, ptr, i);
  134. + dest += i;
  135. + }
  136. + }
  137. + return dest;
  138. +}
  139. +#endif
  140. +
  141. static void merge_fdt_bootargs(void *fdt, const char *fdt_cmdline)
  142. {
  143. char cmdline[COMMAND_LINE_SIZE];
  144. @@ -88,18 +150,28 @@ static void merge_fdt_bootargs(void *fdt
  145. /* and append the ATAG_CMDLINE */
  146. if (fdt_cmdline) {
  147. +
  148. +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
  149. + //save original bootloader args
  150. + //and append ubi.mtd with root partition number to current cmdline
  151. + setprop_string(fdt, "/chosen", "bootloader-args", fdt_cmdline);
  152. + ptr = append_rootblock(ptr, fdt_cmdline, len, fdt);
  153. +
  154. +#else
  155. len = strlen(fdt_cmdline);
  156. if (ptr - cmdline + len + 2 < COMMAND_LINE_SIZE) {
  157. *ptr++ = ' ';
  158. memcpy(ptr, fdt_cmdline, len);
  159. ptr += len;
  160. }
  161. +#endif
  162. }
  163. *ptr = '\0';
  164. setprop_string(fdt, "/chosen", "bootargs", cmdline);
  165. }
  166. +#ifndef CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
  167. static void hex_str(char *out, uint32_t value)
  168. {
  169. uint32_t digit;
  170. @@ -117,6 +189,7 @@ static void hex_str(char *out, uint32_t
  171. }
  172. *out = '\0';
  173. }
  174. +#endif
  175. /*
  176. * Convert and fold provided ATAGs into the provided FDT.
  177. @@ -131,9 +204,11 @@ int atags_to_fdt(void *atag_list, void *
  178. struct tag *atag = atag_list;
  179. /* In the case of 64 bits memory size, need to reserve 2 cells for
  180. * address and size for each bank */
  181. +#ifndef CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
  182. __be32 mem_reg_property[2 * 2 * NR_BANKS];
  183. - int memcount = 0;
  184. - int ret, memsize;
  185. + int memsize, memcount = 0;
  186. +#endif
  187. + int ret;
  188. /* make sure we've got an aligned pointer */
  189. if ((u32)atag_list & 0x3)
  190. @@ -168,7 +243,9 @@ int atags_to_fdt(void *atag_list, void *
  191. else
  192. setprop_string(fdt, "/chosen", "bootargs",
  193. atag->u.cmdline.cmdline);
  194. - } else if (atag->hdr.tag == ATAG_MEM) {
  195. + }
  196. +#ifndef CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
  197. + else if (atag->hdr.tag == ATAG_MEM) {
  198. if (memcount >= sizeof(mem_reg_property)/4)
  199. continue;
  200. if (!atag->u.mem.size)
  201. @@ -212,6 +289,10 @@ int atags_to_fdt(void *atag_list, void *
  202. setprop(fdt, "/memory", "reg", mem_reg_property,
  203. 4 * memcount * memsize);
  204. }
  205. +#else
  206. +
  207. + }
  208. +#endif
  209. return fdt_pack(fdt);
  210. }
  211. --- a/init/main.c
  212. +++ b/init/main.c
  213. @@ -114,6 +114,10 @@
  214. #include <kunit/test.h>
  215. +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
  216. +#include <linux/of.h>
  217. +#endif
  218. +
  219. static int kernel_init(void *);
  220. extern void init_IRQ(void);
  221. @@ -991,6 +995,18 @@ asmlinkage __visible void __init __no_sa
  222. page_alloc_init();
  223. pr_notice("Kernel command line: %s\n", saved_command_line);
  224. +
  225. +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
  226. + //Show bootloader's original command line for reference
  227. + if(of_chosen) {
  228. + const char *prop = of_get_property(of_chosen, "bootloader-args", NULL);
  229. + if(prop)
  230. + pr_notice("Bootloader command line (ignored): %s\n", prop);
  231. + else
  232. + pr_notice("Bootloader command line not present\n");
  233. + }
  234. +#endif
  235. +
  236. /* parameters may set static keys */
  237. jump_label_init();
  238. parse_early_param();