main.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (C) Paul Mackerras 1997.
  3. *
  4. * Updates for PPC64 by Todd Inglett, Dave Engebretsen & Peter Bergner.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <stdarg.h>
  12. #include <stddef.h>
  13. #include "elf.h"
  14. #include "page.h"
  15. #include "string.h"
  16. #include "stdio.h"
  17. #include "ops.h"
  18. #include "gunzip_util.h"
  19. #include "reg.h"
  20. static struct gunzip_state gzstate;
  21. struct addr_range {
  22. void *addr;
  23. unsigned long size;
  24. };
  25. #undef DEBUG
  26. static struct addr_range prep_kernel(void)
  27. {
  28. char elfheader[256];
  29. void *vmlinuz_addr = _vmlinux_start;
  30. unsigned long vmlinuz_size = _vmlinux_end - _vmlinux_start;
  31. void *addr = 0;
  32. struct elf_info ei;
  33. int len;
  34. /* gunzip the ELF header of the kernel */
  35. gunzip_start(&gzstate, vmlinuz_addr, vmlinuz_size);
  36. gunzip_exactly(&gzstate, elfheader, sizeof(elfheader));
  37. if (!parse_elf64(elfheader, &ei) && !parse_elf32(elfheader, &ei))
  38. fatal("Error: not a valid PPC32 or PPC64 ELF file!\n\r");
  39. if (platform_ops.image_hdr)
  40. platform_ops.image_hdr(elfheader);
  41. /* We need to alloc the memsize: gzip will expand the kernel
  42. * text/data, then possible rubbish we don't care about. But
  43. * the kernel bss must be claimed (it will be zero'd by the
  44. * kernel itself)
  45. */
  46. printf("Allocating 0x%lx bytes for kernel ...\n\r", ei.memsize);
  47. if (platform_ops.vmlinux_alloc) {
  48. addr = platform_ops.vmlinux_alloc(ei.memsize);
  49. } else {
  50. /*
  51. * Check if the kernel image (without bss) would overwrite the
  52. * bootwrapper. The device tree has been moved in fdt_init()
  53. * to an area allocated with malloc() (somewhere past _end).
  54. */
  55. if ((unsigned long)_start < ei.loadsize)
  56. fatal("Insufficient memory for kernel at address 0!"
  57. " (_start=%p, uncompressed size=%08lx)\n\r",
  58. _start, ei.loadsize);
  59. if ((unsigned long)_end < ei.memsize)
  60. fatal("The final kernel image would overwrite the "
  61. "device tree\n\r");
  62. }
  63. /* Finally, gunzip the kernel */
  64. printf("gunzipping (0x%p <- 0x%p:0x%p)...", addr,
  65. vmlinuz_addr, vmlinuz_addr+vmlinuz_size);
  66. /* discard up to the actual load data */
  67. gunzip_discard(&gzstate, ei.elfoffset - sizeof(elfheader));
  68. len = gunzip_finish(&gzstate, addr, ei.loadsize);
  69. if (len != ei.loadsize)
  70. fatal("ran out of data! only got 0x%x of 0x%lx bytes.\n\r",
  71. len, ei.loadsize);
  72. printf("done 0x%x bytes\n\r", len);
  73. flush_cache(addr, ei.loadsize);
  74. return (struct addr_range){addr, ei.memsize};
  75. }
  76. static struct addr_range prep_initrd(struct addr_range vmlinux, void *chosen,
  77. unsigned long initrd_addr,
  78. unsigned long initrd_size)
  79. {
  80. /* If we have an image attached to us, it overrides anything
  81. * supplied by the loader. */
  82. if (_initrd_end > _initrd_start) {
  83. printf("Attached initrd image at 0x%p-0x%p\n\r",
  84. _initrd_start, _initrd_end);
  85. initrd_addr = (unsigned long)_initrd_start;
  86. initrd_size = _initrd_end - _initrd_start;
  87. } else if (initrd_size > 0) {
  88. printf("Using loader supplied ramdisk at 0x%lx-0x%lx\n\r",
  89. initrd_addr, initrd_addr + initrd_size);
  90. }
  91. /* If there's no initrd at all, we're done */
  92. if (! initrd_size)
  93. return (struct addr_range){0, 0};
  94. /*
  95. * If the initrd is too low it will be clobbered when the
  96. * kernel relocates to its final location. In this case,
  97. * allocate a safer place and move it.
  98. */
  99. if (initrd_addr < vmlinux.size) {
  100. void *old_addr = (void *)initrd_addr;
  101. printf("Allocating 0x%lx bytes for initrd ...\n\r",
  102. initrd_size);
  103. initrd_addr = (unsigned long)malloc(initrd_size);
  104. if (! initrd_addr)
  105. fatal("Can't allocate memory for initial "
  106. "ramdisk !\n\r");
  107. printf("Relocating initrd 0x%lx <- 0x%p (0x%lx bytes)\n\r",
  108. initrd_addr, old_addr, initrd_size);
  109. memmove((void *)initrd_addr, old_addr, initrd_size);
  110. }
  111. printf("initrd head: 0x%lx\n\r", *((unsigned long *)initrd_addr));
  112. /* Tell the kernel initrd address via device tree */
  113. setprop_val(chosen, "linux,initrd-start", (u32)(initrd_addr));
  114. setprop_val(chosen, "linux,initrd-end", (u32)(initrd_addr+initrd_size));
  115. return (struct addr_range){(void *)initrd_addr, initrd_size};
  116. }
  117. /* A buffer that may be edited by tools operating on a zImage binary so as to
  118. * edit the command line passed to vmlinux (by setting /chosen/bootargs).
  119. * The buffer is put in it's own section so that tools may locate it easier.
  120. */
  121. static char cmdline[BOOT_COMMAND_LINE_SIZE]
  122. __attribute__((__section__("__builtin_cmdline")));
  123. static void prep_cmdline(void *chosen)
  124. {
  125. unsigned int getline_timeout = 5000;
  126. int v;
  127. int n;
  128. /* Wait-for-input time */
  129. n = getprop(chosen, "linux,cmdline-timeout", &v, sizeof(v));
  130. if (n == sizeof(v))
  131. getline_timeout = v;
  132. if (cmdline[0] == '\0')
  133. getprop(chosen, "bootargs", cmdline, BOOT_COMMAND_LINE_SIZE-1);
  134. printf("\n\rLinux/PowerPC load: %s", cmdline);
  135. /* If possible, edit the command line */
  136. if (console_ops.edit_cmdline && getline_timeout)
  137. console_ops.edit_cmdline(cmdline, BOOT_COMMAND_LINE_SIZE, getline_timeout);
  138. printf("\n\r");
  139. /* Put the command line back into the devtree for the kernel */
  140. setprop_str(chosen, "bootargs", cmdline);
  141. }
  142. struct platform_ops platform_ops;
  143. struct dt_ops dt_ops;
  144. struct console_ops console_ops;
  145. struct loader_info loader_info;
  146. void start(void)
  147. {
  148. struct addr_range vmlinux, initrd;
  149. kernel_entry_t kentry;
  150. unsigned long ft_addr = 0;
  151. void *chosen;
  152. /* Do this first, because malloc() could clobber the loader's
  153. * command line. Only use the loader command line if a
  154. * built-in command line wasn't set by an external tool */
  155. if ((loader_info.cmdline_len > 0) && (cmdline[0] == '\0'))
  156. memmove(cmdline, loader_info.cmdline,
  157. min(loader_info.cmdline_len, BOOT_COMMAND_LINE_SIZE-1));
  158. if (console_ops.open && (console_ops.open() < 0))
  159. exit();
  160. if (platform_ops.fixups)
  161. platform_ops.fixups();
  162. printf("\n\rzImage starting: loaded at 0x%p (sp: 0x%p)\n\r",
  163. _start, get_sp());
  164. /* Ensure that the device tree has a /chosen node */
  165. chosen = finddevice("/chosen");
  166. if (!chosen)
  167. chosen = create_node(NULL, "chosen");
  168. vmlinux = prep_kernel();
  169. initrd = prep_initrd(vmlinux, chosen,
  170. loader_info.initrd_addr, loader_info.initrd_size);
  171. prep_cmdline(chosen);
  172. printf("Finalizing device tree...");
  173. if (dt_ops.finalize)
  174. ft_addr = dt_ops.finalize();
  175. if (ft_addr)
  176. printf(" flat tree at 0x%lx\n\r", ft_addr);
  177. else
  178. printf(" using OF tree (promptr=%p)\n\r", loader_info.promptr);
  179. if (console_ops.close)
  180. console_ops.close();
  181. kentry = (kernel_entry_t) vmlinux.addr;
  182. if (ft_addr)
  183. kentry(ft_addr, 0, NULL);
  184. else
  185. kentry((unsigned long)initrd.addr, initrd.size,
  186. loader_info.promptr);
  187. /* console closed so printf in fatal below may not work */
  188. fatal("Error: Linux kernel returned to zImage boot wrapper!\n\r");
  189. }