main.c 7.3 KB

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