main.c 6.5 KB

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