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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. @@ -69,6 +71,80 @@ static uint32_t get_cell_size(const void
  49. return cell_size;
  50. }
  51. +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
  52. +/**
  53. + * taken from arch/x86/boot/string.c
  54. + * local_strstr - Find the first substring in a %NUL terminated string
  55. + * @s1: The string to be searched
  56. + * @s2: The string to search for
  57. + */
  58. +static char *local_strstr(const char *s1, const char *s2)
  59. +{
  60. + size_t l1, l2;
  61. +
  62. + l2 = strlen(s2);
  63. + if (!l2)
  64. + return (char *)s1;
  65. + l1 = strlen(s1);
  66. + while (l1 >= l2) {
  67. + l1--;
  68. + if (!memcmp(s1, s2, l2))
  69. + return (char *)s1;
  70. + s1++;
  71. + }
  72. + return NULL;
  73. +}
  74. +
  75. +static char *append_rootblock(char *dest, const char *str, int len, void *fdt)
  76. +{
  77. + char *ptr, *end, *tmp;
  78. + char *root="root=";
  79. + char *find_rootblock;
  80. + int i, l;
  81. + const char *rootblock;
  82. +
  83. + find_rootblock = getprop(fdt, "/chosen", "find-rootblock", &l);
  84. + if(!find_rootblock)
  85. + find_rootblock = root;
  86. +
  87. + //ARM doesn't have __HAVE_ARCH_STRSTR, so it was copied from x86
  88. + ptr = local_strstr(str, find_rootblock);
  89. +
  90. + if(!ptr)
  91. + return dest;
  92. +
  93. + end = strchr(ptr, ' ');
  94. + end = end ? (end - 1) : (strchr(ptr, 0) - 1);
  95. +
  96. + // Some boards ubi.mtd=XX,ZZZZ, so let's check for '," too.
  97. + tmp = strchr(ptr, ',');
  98. +
  99. + if(tmp)
  100. + end = end < tmp ? end : tmp - 1;
  101. +
  102. + //find partition number (assumes format root=/dev/mtdXX | /dev/mtdblockXX | yy:XX | ubi.mtd=XX,ZZZZ )
  103. + for( i = 0; end >= ptr && *end >= '0' && *end <= '9'; end--, i++);
  104. + ptr = end + 1;
  105. +
  106. + /* if append-rootblock property is set use it to append to command line */
  107. + rootblock = getprop(fdt, "/chosen", "append-rootblock", &l);
  108. + if(rootblock != NULL) {
  109. + if(*dest != ' ') {
  110. + *dest = ' ';
  111. + dest++;
  112. + len++;
  113. + }
  114. + if (len + l + i <= COMMAND_LINE_SIZE) {
  115. + memcpy(dest, rootblock, l);
  116. + dest += l - 1;
  117. + memcpy(dest, ptr, i);
  118. + dest += i;
  119. + }
  120. + }
  121. + return dest;
  122. +}
  123. +#endif
  124. +
  125. static void merge_fdt_bootargs(void *fdt, const char *fdt_cmdline)
  126. {
  127. char cmdline[COMMAND_LINE_SIZE];
  128. @@ -88,12 +164,21 @@ static void merge_fdt_bootargs(void *fdt
  129. /* and append the ATAG_CMDLINE */
  130. if (fdt_cmdline) {
  131. +
  132. +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
  133. + //save original bootloader args
  134. + //and append ubi.mtd with root partition number to current cmdline
  135. + setprop_string(fdt, "/chosen", "bootloader-args", fdt_cmdline);
  136. + ptr = append_rootblock(ptr, fdt_cmdline, len, fdt);
  137. +
  138. +#else
  139. len = strlen(fdt_cmdline);
  140. if (ptr - cmdline + len + 2 < COMMAND_LINE_SIZE) {
  141. *ptr++ = ' ';
  142. memcpy(ptr, fdt_cmdline, len);
  143. ptr += len;
  144. }
  145. +#endif
  146. }
  147. *ptr = '\0';
  148. @@ -168,7 +253,9 @@ int atags_to_fdt(void *atag_list, void *
  149. else
  150. setprop_string(fdt, "/chosen", "bootargs",
  151. atag->u.cmdline.cmdline);
  152. - } else if (atag->hdr.tag == ATAG_MEM) {
  153. + }
  154. +#ifndef CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
  155. + else if (atag->hdr.tag == ATAG_MEM) {
  156. if (memcount >= sizeof(mem_reg_property)/4)
  157. continue;
  158. if (!atag->u.mem.size)
  159. @@ -212,6 +299,10 @@ int atags_to_fdt(void *atag_list, void *
  160. setprop(fdt, "/memory", "reg", mem_reg_property,
  161. 4 * memcount * memsize);
  162. }
  163. +#else
  164. +
  165. + }
  166. +#endif
  167. return fdt_pack(fdt);
  168. }
  169. --- a/init/main.c
  170. +++ b/init/main.c
  171. @@ -114,6 +114,10 @@
  172. #include <kunit/test.h>
  173. +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
  174. +#include <linux/of.h>
  175. +#endif
  176. +
  177. static int kernel_init(void *);
  178. extern void init_IRQ(void);
  179. @@ -993,6 +997,18 @@ asmlinkage __visible void __init __no_sa
  180. pr_notice("Kernel command line: %s\n", saved_command_line);
  181. /* parameters may set static keys */
  182. jump_label_init();
  183. +
  184. +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
  185. + //Show bootloader's original command line for reference
  186. + if(of_chosen) {
  187. + const char *prop = of_get_property(of_chosen, "bootloader-args", NULL);
  188. + if(prop)
  189. + pr_notice("Bootloader command line (ignored): %s\n", prop);
  190. + else
  191. + pr_notice("Bootloader command line not present\n");
  192. + }
  193. +#endif
  194. +
  195. parse_early_param();
  196. after_dashes = parse_args("Booting kernel",
  197. static_command_line, __start___param,