1
0

006-mvebu-Mangle-bootloader-s-kernel-arguments.patch 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. From 71270226b14733a4b1f2cde58ea9265caa50b38d Mon Sep 17 00:00:00 2001
  2. From: Adrian Panella <ianchi74@outlook.com>
  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 <ianchi74@outlook.com>
  15. This patch has been modified to be mvebu specific. The original patch
  16. did not pass the bootloader cmdline on if no append-rootblock stanza
  17. was found, resulting in blank cmdline and failure to boot.
  18. Signed-off-by: Michael Gray <michael.gray@lantisproject.com>
  19. ---
  20. arch/arm/Kconfig | 11 +++++
  21. arch/arm/boot/compressed/atags_to_fdt.c | 72 ++++++++++++++++++++++++++++++++-
  22. init/main.c | 16 ++++++++
  23. 3 files changed, 98 insertions(+), 1 deletion(-)
  24. --- a/arch/arm/Kconfig
  25. +++ b/arch/arm/Kconfig
  26. @@ -1937,6 +1937,17 @@ config ARM_ATAG_DTB_COMPAT_CMDLINE_EXTEN
  27. The command-line arguments provided by the boot loader will be
  28. appended to the the device tree bootargs property.
  29. +config ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
  30. + bool "Append rootblock parsing bootloader's kernel arguments"
  31. + help
  32. + The command-line arguments provided by the boot loader will be
  33. + appended to a new device tree property: bootloader-args.
  34. + If there is a property "append-rootblock" in DT under /chosen
  35. + and a root= option in bootloaders command line it will be parsed
  36. + and added to DT bootargs with the form: <append-rootblock>XX.
  37. + Only command line ATAG will be processed, the rest of the ATAGs
  38. + sent by bootloader will be ignored.
  39. +
  40. endchoice
  41. config CMDLINE
  42. --- a/arch/arm/boot/compressed/atags_to_fdt.c
  43. +++ b/arch/arm/boot/compressed/atags_to_fdt.c
  44. @@ -4,6 +4,8 @@
  45. #if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_EXTEND)
  46. #define do_extend_cmdline 1
  47. +#elif defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
  48. +#define do_extend_cmdline 1
  49. #else
  50. #define do_extend_cmdline 0
  51. #endif
  52. @@ -67,6 +69,65 @@ static uint32_t get_cell_size(const void
  53. return cell_size;
  54. }
  55. +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
  56. +
  57. +static char *append_rootblock(char *dest, const char *str, int len, void *fdt)
  58. +{
  59. + char *ptr, *end;
  60. + char *root="root=";
  61. + int i, l;
  62. + const char *rootblock;
  63. +
  64. + //ARM doesn't have __HAVE_ARCH_STRSTR, so search manually
  65. + ptr = str - 1;
  66. +
  67. + do {
  68. + //first find an 'r' at the begining or after a space
  69. + do {
  70. + ptr++;
  71. + ptr = strchr(ptr, 'r');
  72. + if(!ptr) return dest;
  73. +
  74. + } while (ptr != str && *(ptr-1) != ' ');
  75. +
  76. + //then check for the rest
  77. + for(i = 1; i <= 4; i++)
  78. + if(*(ptr+i) != *(root+i)) break;
  79. +
  80. + } while (i != 5);
  81. +
  82. + end = strchr(ptr, ' ');
  83. + end = end ? (end - 1) : (strchr(ptr, 0) - 1);
  84. +
  85. + //find partition number (assumes format root=/dev/mtdXX | /dev/mtdblockXX | yy:XX )
  86. + for( i = 0; end >= ptr && *end >= '0' && *end <= '9'; end--, i++);
  87. + ptr = end + 1;
  88. +
  89. + /* if append-rootblock property is set use it to append to command line */
  90. + rootblock = getprop(fdt, "/chosen", "append-rootblock", &l);
  91. + if(rootblock != NULL) {
  92. + if(*dest != ' ') {
  93. + *dest = ' ';
  94. + dest++;
  95. + len++;
  96. + }
  97. + if (len + l + i <= COMMAND_LINE_SIZE) {
  98. + memcpy(dest, rootblock, l);
  99. + dest += l - 1;
  100. + memcpy(dest, ptr, i);
  101. + dest += i;
  102. + }
  103. + } else {
  104. + len = strlen(str);
  105. + if (len + 1 < COMMAND_LINE_SIZE) {
  106. + memcpy(dest, str, len);
  107. + dest += len;
  108. + }
  109. + }
  110. + return dest;
  111. +}
  112. +#endif
  113. +
  114. static void merge_fdt_bootargs(void *fdt, const char *fdt_cmdline)
  115. {
  116. char cmdline[COMMAND_LINE_SIZE];
  117. @@ -86,12 +147,21 @@ static void merge_fdt_bootargs(void *fdt
  118. /* and append the ATAG_CMDLINE */
  119. if (fdt_cmdline) {
  120. +
  121. +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
  122. + //save original bootloader args
  123. + //and append ubi.mtd with root partition number to current cmdline
  124. + setprop_string(fdt, "/chosen", "bootloader-args", fdt_cmdline);
  125. + ptr = append_rootblock(ptr, fdt_cmdline, len, fdt);
  126. +
  127. +#else
  128. len = strlen(fdt_cmdline);
  129. if (ptr - cmdline + len + 2 < COMMAND_LINE_SIZE) {
  130. *ptr++ = ' ';
  131. memcpy(ptr, fdt_cmdline, len);
  132. ptr += len;
  133. }
  134. +#endif
  135. }
  136. *ptr = '\0';
  137. @@ -148,7 +218,9 @@ int atags_to_fdt(void *atag_list, void *
  138. else
  139. setprop_string(fdt, "/chosen", "bootargs",
  140. atag->u.cmdline.cmdline);
  141. - } else if (atag->hdr.tag == ATAG_MEM) {
  142. + }
  143. +#ifndef CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
  144. + else if (atag->hdr.tag == ATAG_MEM) {
  145. if (memcount >= sizeof(mem_reg_property)/4)
  146. continue;
  147. if (!atag->u.mem.size)
  148. @@ -187,6 +259,10 @@ int atags_to_fdt(void *atag_list, void *
  149. setprop(fdt, "/memory", "reg", mem_reg_property,
  150. 4 * memcount * memsize);
  151. }
  152. +#else
  153. +
  154. + }
  155. +#endif
  156. return fdt_pack(fdt);
  157. }
  158. --- a/init/main.c
  159. +++ b/init/main.c
  160. @@ -95,6 +95,10 @@
  161. #include <asm/sections.h>
  162. #include <asm/cacheflush.h>
  163. +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
  164. +#include <linux/of.h>
  165. +#endif
  166. +
  167. static int kernel_init(void *);
  168. extern void init_IRQ(void);
  169. @@ -566,6 +570,18 @@ asmlinkage __visible void __init start_k
  170. page_alloc_init();
  171. pr_notice("Kernel command line: %s\n", boot_command_line);
  172. +
  173. +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
  174. + //Show bootloader's original command line for reference
  175. + if(of_chosen) {
  176. + const char *prop = of_get_property(of_chosen, "bootloader-args", NULL);
  177. + if(prop)
  178. + pr_notice("Bootloader command line (ignored): %s\n", prop);
  179. + else
  180. + pr_notice("Bootloader command line not present\n");
  181. + }
  182. +#endif
  183. +
  184. /* parameters may set static keys */
  185. jump_label_init();
  186. parse_early_param();