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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. @@ -1939,6 +1939,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. @@ -4,6 +4,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. @@ -67,6 +69,59 @@ 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. +static char *append_rootblock(char *dest, const char *str, int len, void *fdt)
  54. +{
  55. + char *ptr, *end;
  56. + char *root="root=";
  57. + int i, l;
  58. + const char *rootblock;
  59. +
  60. + //ARM doesn't have __HAVE_ARCH_STRSTR, so search manually
  61. + ptr = str - 1;
  62. +
  63. + do {
  64. + //first find an 'r' at the begining or after a space
  65. + do {
  66. + ptr++;
  67. + ptr = strchr(ptr, 'r');
  68. + if(!ptr) return dest;
  69. +
  70. + } while (ptr != str && *(ptr-1) != ' ');
  71. +
  72. + //then check for the rest
  73. + for(i = 1; i <= 4; i++)
  74. + if(*(ptr+i) != *(root+i)) break;
  75. +
  76. + } while (i != 5);
  77. +
  78. + end = strchr(ptr, ' ');
  79. + end = end ? (end - 1) : (strchr(ptr, 0) - 1);
  80. +
  81. + //find partition number (assumes format root=/dev/mtdXX | /dev/mtdblockXX | yy:XX )
  82. + for( i = 0; end >= ptr && *end >= '0' && *end <= '9'; end--, i++);
  83. + ptr = end + 1;
  84. +
  85. + /* if append-rootblock property is set use it to append to command line */
  86. + rootblock = getprop(fdt, "/chosen", "append-rootblock", &l);
  87. + if(rootblock != NULL) {
  88. + if(*dest != ' ') {
  89. + *dest = ' ';
  90. + dest++;
  91. + len++;
  92. + }
  93. + if (len + l + i <= COMMAND_LINE_SIZE) {
  94. + memcpy(dest, rootblock, l);
  95. + dest += l - 1;
  96. + memcpy(dest, ptr, i);
  97. + dest += i;
  98. + }
  99. + }
  100. + return dest;
  101. +}
  102. +#endif
  103. +
  104. static void merge_fdt_bootargs(void *fdt, const char *fdt_cmdline)
  105. {
  106. char cmdline[COMMAND_LINE_SIZE];
  107. @@ -86,12 +141,21 @@ static void merge_fdt_bootargs(void *fdt
  108. /* and append the ATAG_CMDLINE */
  109. if (fdt_cmdline) {
  110. +
  111. +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
  112. + //save original bootloader args
  113. + //and append ubi.mtd with root partition number to current cmdline
  114. + setprop_string(fdt, "/chosen", "bootloader-args", fdt_cmdline);
  115. + ptr = append_rootblock(ptr, fdt_cmdline, len, fdt);
  116. +
  117. +#else
  118. len = strlen(fdt_cmdline);
  119. if (ptr - cmdline + len + 2 < COMMAND_LINE_SIZE) {
  120. *ptr++ = ' ';
  121. memcpy(ptr, fdt_cmdline, len);
  122. ptr += len;
  123. }
  124. +#endif
  125. }
  126. *ptr = '\0';
  127. @@ -148,7 +212,9 @@ int atags_to_fdt(void *atag_list, void *
  128. else
  129. setprop_string(fdt, "/chosen", "bootargs",
  130. atag->u.cmdline.cmdline);
  131. - } else if (atag->hdr.tag == ATAG_MEM) {
  132. + }
  133. +#ifndef CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
  134. + else if (atag->hdr.tag == ATAG_MEM) {
  135. if (memcount >= sizeof(mem_reg_property)/4)
  136. continue;
  137. if (!atag->u.mem.size)
  138. @@ -187,6 +253,10 @@ int atags_to_fdt(void *atag_list, void *
  139. setprop(fdt, "/memory", "reg", mem_reg_property,
  140. 4 * memcount * memsize);
  141. }
  142. +#else
  143. +
  144. + }
  145. +#endif
  146. return fdt_pack(fdt);
  147. }
  148. --- a/init/main.c
  149. +++ b/init/main.c
  150. @@ -95,6 +95,10 @@
  151. #include <asm/sections.h>
  152. #include <asm/cacheflush.h>
  153. +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
  154. +#include <linux/of.h>
  155. +#endif
  156. +
  157. static int kernel_init(void *);
  158. extern void init_IRQ(void);
  159. @@ -574,6 +578,18 @@ asmlinkage __visible void __init start_k
  160. page_alloc_init();
  161. pr_notice("Kernel command line: %s\n", boot_command_line);
  162. +
  163. +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
  164. + //Show bootloader's original command line for reference
  165. + if(of_chosen) {
  166. + const char *prop = of_get_property(of_chosen, "bootloader-args", NULL);
  167. + if(prop)
  168. + pr_notice("Bootloader command line (ignored): %s\n", prop);
  169. + else
  170. + pr_notice("Bootloader command line not present\n");
  171. + }
  172. +#endif
  173. +
  174. parse_early_param();
  175. after_dashes = parse_args("Booting kernel",
  176. static_command_line, __start___param,