bootpz.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * arch/alpha/boot/bootpz.c
  4. *
  5. * Copyright (C) 1997 Jay Estabrook
  6. *
  7. * This file is used for creating a compressed BOOTP file for the
  8. * Linux/AXP kernel
  9. *
  10. * based significantly on the arch/alpha/boot/main.c of Linus Torvalds
  11. * and the decompression code from MILO.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/string.h>
  16. #include <generated/utsrelease.h>
  17. #include <linux/mm.h>
  18. #include <asm/console.h>
  19. #include <asm/hwrpb.h>
  20. #include <asm/pgtable.h>
  21. #include <asm/io.h>
  22. #include <stdarg.h>
  23. #include "kzsize.h"
  24. /* FIXME FIXME FIXME */
  25. #define MALLOC_AREA_SIZE 0x200000 /* 2MB for now */
  26. /* FIXME FIXME FIXME */
  27. /*
  28. WARNING NOTE
  29. It is very possible that turning on additional messages may cause
  30. kernel image corruption due to stack usage to do the printing.
  31. */
  32. #undef DEBUG_CHECK_RANGE
  33. #undef DEBUG_ADDRESSES
  34. #undef DEBUG_LAST_STEPS
  35. extern unsigned long switch_to_osf_pal(unsigned long nr,
  36. struct pcb_struct * pcb_va, struct pcb_struct * pcb_pa,
  37. unsigned long *vptb);
  38. extern int decompress_kernel(void* destination, void *source,
  39. size_t ksize, size_t kzsize);
  40. extern void move_stack(unsigned long new_stack);
  41. struct hwrpb_struct *hwrpb = INIT_HWRPB;
  42. static struct pcb_struct pcb_va[1];
  43. /*
  44. * Find a physical address of a virtual object..
  45. *
  46. * This is easy using the virtual page table address.
  47. */
  48. #define VPTB ((unsigned long *) 0x200000000)
  49. static inline unsigned long
  50. find_pa(unsigned long address)
  51. {
  52. unsigned long result;
  53. result = VPTB[address >> 13];
  54. result >>= 32;
  55. result <<= 13;
  56. result |= address & 0x1fff;
  57. return result;
  58. }
  59. int
  60. check_range(unsigned long vstart, unsigned long vend,
  61. unsigned long kstart, unsigned long kend)
  62. {
  63. unsigned long vaddr, kaddr;
  64. #ifdef DEBUG_CHECK_RANGE
  65. srm_printk("check_range: V[0x%lx:0x%lx] K[0x%lx:0x%lx]\n",
  66. vstart, vend, kstart, kend);
  67. #endif
  68. /* do some range checking for detecting an overlap... */
  69. for (vaddr = vstart; vaddr <= vend; vaddr += PAGE_SIZE)
  70. {
  71. kaddr = (find_pa(vaddr) | PAGE_OFFSET);
  72. if (kaddr >= kstart && kaddr <= kend)
  73. {
  74. #ifdef DEBUG_CHECK_RANGE
  75. srm_printk("OVERLAP: vaddr 0x%lx kaddr 0x%lx"
  76. " [0x%lx:0x%lx]\n",
  77. vaddr, kaddr, kstart, kend);
  78. #endif
  79. return 1;
  80. }
  81. }
  82. return 0;
  83. }
  84. /*
  85. * This function moves into OSF/1 pal-code, and has a temporary
  86. * PCB for that. The kernel proper should replace this PCB with
  87. * the real one as soon as possible.
  88. *
  89. * The page table muckery in here depends on the fact that the boot
  90. * code has the L1 page table identity-map itself in the second PTE
  91. * in the L1 page table. Thus the L1-page is virtually addressable
  92. * itself (through three levels) at virtual address 0x200802000.
  93. */
  94. #define L1 ((unsigned long *) 0x200802000)
  95. void
  96. pal_init(void)
  97. {
  98. unsigned long i, rev;
  99. struct percpu_struct * percpu;
  100. struct pcb_struct * pcb_pa;
  101. /* Create the dummy PCB. */
  102. pcb_va->ksp = 0;
  103. pcb_va->usp = 0;
  104. pcb_va->ptbr = L1[1] >> 32;
  105. pcb_va->asn = 0;
  106. pcb_va->pcc = 0;
  107. pcb_va->unique = 0;
  108. pcb_va->flags = 1;
  109. pcb_va->res1 = 0;
  110. pcb_va->res2 = 0;
  111. pcb_pa = (struct pcb_struct *)find_pa((unsigned long)pcb_va);
  112. /*
  113. * a0 = 2 (OSF)
  114. * a1 = return address, but we give the asm the vaddr of the PCB
  115. * a2 = physical addr of PCB
  116. * a3 = new virtual page table pointer
  117. * a4 = KSP (but the asm sets it)
  118. */
  119. srm_printk("Switching to OSF PAL-code... ");
  120. i = switch_to_osf_pal(2, pcb_va, pcb_pa, VPTB);
  121. if (i) {
  122. srm_printk("failed, code %ld\n", i);
  123. __halt();
  124. }
  125. percpu = (struct percpu_struct *)
  126. (INIT_HWRPB->processor_offset + (unsigned long) INIT_HWRPB);
  127. rev = percpu->pal_revision = percpu->palcode_avail[2];
  128. srm_printk("OK (rev %lx)\n", rev);
  129. tbia(); /* do it directly in case we are SMP */
  130. }
  131. /*
  132. * Start the kernel.
  133. */
  134. static inline void
  135. runkernel(void)
  136. {
  137. __asm__ __volatile__(
  138. "bis %0,%0,$27\n\t"
  139. "jmp ($27)"
  140. : /* no outputs: it doesn't even return */
  141. : "r" (START_ADDR));
  142. }
  143. /* Must record the SP (it is virtual) on entry, so we can make sure
  144. not to overwrite it during movement or decompression. */
  145. unsigned long SP_on_entry;
  146. /* Calculate the kernel image address based on the end of the BOOTP
  147. bootstrapper (ie this program).
  148. */
  149. extern char _end;
  150. #define KERNEL_ORIGIN \
  151. ((((unsigned long)&_end) + 511) & ~511)
  152. /* Round address to next higher page boundary. */
  153. #define NEXT_PAGE(a) (((a) | (PAGE_SIZE - 1)) + 1)
  154. #ifdef INITRD_IMAGE_SIZE
  155. # define REAL_INITRD_SIZE INITRD_IMAGE_SIZE
  156. #else
  157. # define REAL_INITRD_SIZE 0
  158. #endif
  159. /* Defines from include/asm-alpha/system.h
  160. BOOT_ADDR Virtual address at which the consoles loads
  161. the BOOTP image.
  162. KERNEL_START KSEG address at which the kernel is built to run,
  163. which includes some initial data pages before the
  164. code.
  165. START_ADDR KSEG address of the entry point of kernel code.
  166. ZERO_PGE KSEG address of page full of zeroes, but
  167. upon entry to kerne cvan be expected
  168. to hold the parameter list and possible
  169. INTRD information.
  170. These are used in the local defines below.
  171. */
  172. /* Virtual addresses for the BOOTP image. Note that this includes the
  173. bootstrapper code as well as the compressed kernel image, and
  174. possibly the INITRD image.
  175. Oh, and do NOT forget the STACK, which appears to be placed virtually
  176. beyond the end of the loaded image.
  177. */
  178. #define V_BOOT_IMAGE_START BOOT_ADDR
  179. #define V_BOOT_IMAGE_END SP_on_entry
  180. /* Virtual addresses for just the bootstrapper part of the BOOTP image. */
  181. #define V_BOOTSTRAPPER_START BOOT_ADDR
  182. #define V_BOOTSTRAPPER_END KERNEL_ORIGIN
  183. /* Virtual addresses for just the data part of the BOOTP
  184. image. This may also include the INITRD image, but always
  185. includes the STACK.
  186. */
  187. #define V_DATA_START KERNEL_ORIGIN
  188. #define V_INITRD_START (KERNEL_ORIGIN + KERNEL_Z_SIZE)
  189. #define V_INTRD_END (V_INITRD_START + REAL_INITRD_SIZE)
  190. #define V_DATA_END V_BOOT_IMAGE_END
  191. /* KSEG addresses for the uncompressed kernel.
  192. Note that the end address includes workspace for the decompression.
  193. Note also that the DATA_START address is ZERO_PGE, to which we write
  194. just before jumping to the kernel image at START_ADDR.
  195. */
  196. #define K_KERNEL_DATA_START ZERO_PGE
  197. #define K_KERNEL_IMAGE_START START_ADDR
  198. #define K_KERNEL_IMAGE_END (START_ADDR + KERNEL_SIZE)
  199. /* Define to where we may have to decompress the kernel image, before
  200. we move it to the final position, in case of overlap. This will be
  201. above the final position of the kernel.
  202. Regardless of overlap, we move the INITRD image to the end of this
  203. copy area, because there needs to be a buffer area after the kernel
  204. for "bootmem" anyway.
  205. */
  206. #define K_COPY_IMAGE_START NEXT_PAGE(K_KERNEL_IMAGE_END)
  207. /* Reserve one page below INITRD for the new stack. */
  208. #define K_INITRD_START \
  209. NEXT_PAGE(K_COPY_IMAGE_START + KERNEL_SIZE + PAGE_SIZE)
  210. #define K_COPY_IMAGE_END \
  211. (K_INITRD_START + REAL_INITRD_SIZE + MALLOC_AREA_SIZE)
  212. #define K_COPY_IMAGE_SIZE \
  213. NEXT_PAGE(K_COPY_IMAGE_END - K_COPY_IMAGE_START)
  214. void
  215. start_kernel(void)
  216. {
  217. int must_move = 0;
  218. /* Initialize these for the decompression-in-place situation,
  219. which is the smallest amount of work and most likely to
  220. occur when using the normal START_ADDR of the kernel
  221. (currently set to 16MB, to clear all console code.
  222. */
  223. unsigned long uncompressed_image_start = K_KERNEL_IMAGE_START;
  224. unsigned long uncompressed_image_end = K_KERNEL_IMAGE_END;
  225. unsigned long initrd_image_start = K_INITRD_START;
  226. /*
  227. * Note that this crufty stuff with static and envval
  228. * and envbuf is because:
  229. *
  230. * 1. Frequently, the stack is short, and we don't want to overrun;
  231. * 2. Frequently the stack is where we are going to copy the kernel to;
  232. * 3. A certain SRM console required the GET_ENV output to stack.
  233. * ??? A comment in the aboot sources indicates that the GET_ENV
  234. * destination must be quadword aligned. Might this explain the
  235. * behaviour, rather than requiring output to the stack, which
  236. * seems rather far-fetched.
  237. */
  238. static long nbytes;
  239. static char envval[256] __attribute__((aligned(8)));
  240. register unsigned long asm_sp asm("30");
  241. SP_on_entry = asm_sp;
  242. srm_printk("Linux/Alpha BOOTPZ Loader for Linux " UTS_RELEASE "\n");
  243. /* Validity check the HWRPB. */
  244. if (INIT_HWRPB->pagesize != 8192) {
  245. srm_printk("Expected 8kB pages, got %ldkB\n",
  246. INIT_HWRPB->pagesize >> 10);
  247. return;
  248. }
  249. if (INIT_HWRPB->vptb != (unsigned long) VPTB) {
  250. srm_printk("Expected vptb at %p, got %p\n",
  251. VPTB, (void *)INIT_HWRPB->vptb);
  252. return;
  253. }
  254. /* PALcode (re)initialization. */
  255. pal_init();
  256. /* Get the parameter list from the console environment variable. */
  257. nbytes = callback_getenv(ENV_BOOTED_OSFLAGS, envval, sizeof(envval));
  258. if (nbytes < 0 || nbytes >= sizeof(envval)) {
  259. nbytes = 0;
  260. }
  261. envval[nbytes] = '\0';
  262. #ifdef DEBUG_ADDRESSES
  263. srm_printk("START_ADDR 0x%lx\n", START_ADDR);
  264. srm_printk("KERNEL_ORIGIN 0x%lx\n", KERNEL_ORIGIN);
  265. srm_printk("KERNEL_SIZE 0x%x\n", KERNEL_SIZE);
  266. srm_printk("KERNEL_Z_SIZE 0x%x\n", KERNEL_Z_SIZE);
  267. #endif
  268. /* Since all the SRM consoles load the BOOTP image at virtual
  269. * 0x20000000, we have to ensure that the physical memory
  270. * pages occupied by that image do NOT overlap the physical
  271. * address range where the kernel wants to be run. This
  272. * causes real problems when attempting to cdecompress the
  273. * former into the latter... :-(
  274. *
  275. * So, we may have to decompress/move the kernel/INITRD image
  276. * virtual-to-physical someplace else first before moving
  277. * kernel /INITRD to their final resting places... ;-}
  278. *
  279. * Sigh...
  280. */
  281. /* First, check to see if the range of addresses occupied by
  282. the bootstrapper part of the BOOTP image include any of the
  283. physical pages into which the kernel will be placed for
  284. execution.
  285. We only need check on the final kernel image range, since we
  286. will put the INITRD someplace that we can be sure is not
  287. in conflict.
  288. */
  289. if (check_range(V_BOOTSTRAPPER_START, V_BOOTSTRAPPER_END,
  290. K_KERNEL_DATA_START, K_KERNEL_IMAGE_END))
  291. {
  292. srm_printk("FATAL ERROR: overlap of bootstrapper code\n");
  293. __halt();
  294. }
  295. /* Next, check to see if the range of addresses occupied by
  296. the compressed kernel/INITRD/stack portion of the BOOTP
  297. image include any of the physical pages into which the
  298. decompressed kernel or the INITRD will be placed for
  299. execution.
  300. */
  301. if (check_range(V_DATA_START, V_DATA_END,
  302. K_KERNEL_IMAGE_START, K_COPY_IMAGE_END))
  303. {
  304. #ifdef DEBUG_ADDRESSES
  305. srm_printk("OVERLAP: cannot decompress in place\n");
  306. #endif
  307. uncompressed_image_start = K_COPY_IMAGE_START;
  308. uncompressed_image_end = K_COPY_IMAGE_END;
  309. must_move = 1;
  310. /* Finally, check to see if the range of addresses
  311. occupied by the compressed kernel/INITRD part of
  312. the BOOTP image include any of the physical pages
  313. into which that part is to be copied for
  314. decompression.
  315. */
  316. while (check_range(V_DATA_START, V_DATA_END,
  317. uncompressed_image_start,
  318. uncompressed_image_end))
  319. {
  320. #if 0
  321. uncompressed_image_start += K_COPY_IMAGE_SIZE;
  322. uncompressed_image_end += K_COPY_IMAGE_SIZE;
  323. initrd_image_start += K_COPY_IMAGE_SIZE;
  324. #else
  325. /* Keep as close as possible to end of BOOTP image. */
  326. uncompressed_image_start += PAGE_SIZE;
  327. uncompressed_image_end += PAGE_SIZE;
  328. initrd_image_start += PAGE_SIZE;
  329. #endif
  330. }
  331. }
  332. srm_printk("Starting to load the kernel with args '%s'\n", envval);
  333. #ifdef DEBUG_ADDRESSES
  334. srm_printk("Decompressing the kernel...\n"
  335. "...from 0x%lx to 0x%lx size 0x%x\n",
  336. V_DATA_START,
  337. uncompressed_image_start,
  338. KERNEL_SIZE);
  339. #endif
  340. decompress_kernel((void *)uncompressed_image_start,
  341. (void *)V_DATA_START,
  342. KERNEL_SIZE, KERNEL_Z_SIZE);
  343. /*
  344. * Now, move things to their final positions, if/as required.
  345. */
  346. #ifdef INITRD_IMAGE_SIZE
  347. /* First, we always move the INITRD image, if present. */
  348. #ifdef DEBUG_ADDRESSES
  349. srm_printk("Moving the INITRD image...\n"
  350. " from 0x%lx to 0x%lx size 0x%x\n",
  351. V_INITRD_START,
  352. initrd_image_start,
  353. INITRD_IMAGE_SIZE);
  354. #endif
  355. memcpy((void *)initrd_image_start, (void *)V_INITRD_START,
  356. INITRD_IMAGE_SIZE);
  357. #endif /* INITRD_IMAGE_SIZE */
  358. /* Next, we may have to move the uncompressed kernel to the
  359. final destination.
  360. */
  361. if (must_move) {
  362. #ifdef DEBUG_ADDRESSES
  363. srm_printk("Moving the uncompressed kernel...\n"
  364. "...from 0x%lx to 0x%lx size 0x%x\n",
  365. uncompressed_image_start,
  366. K_KERNEL_IMAGE_START,
  367. (unsigned)KERNEL_SIZE);
  368. #endif
  369. /*
  370. * Move the stack to a safe place to ensure it won't be
  371. * overwritten by kernel image.
  372. */
  373. move_stack(initrd_image_start - PAGE_SIZE);
  374. memcpy((void *)K_KERNEL_IMAGE_START,
  375. (void *)uncompressed_image_start, KERNEL_SIZE);
  376. }
  377. /* Clear the zero page, then move the argument list in. */
  378. #ifdef DEBUG_LAST_STEPS
  379. srm_printk("Preparing ZERO_PGE...\n");
  380. #endif
  381. memset((char*)ZERO_PGE, 0, PAGE_SIZE);
  382. strcpy((char*)ZERO_PGE, envval);
  383. #ifdef INITRD_IMAGE_SIZE
  384. #ifdef DEBUG_LAST_STEPS
  385. srm_printk("Preparing INITRD info...\n");
  386. #endif
  387. /* Finally, set the INITRD paramenters for the kernel. */
  388. ((long *)(ZERO_PGE+256))[0] = initrd_image_start;
  389. ((long *)(ZERO_PGE+256))[1] = INITRD_IMAGE_SIZE;
  390. #endif /* INITRD_IMAGE_SIZE */
  391. #ifdef DEBUG_LAST_STEPS
  392. srm_printk("Doing 'runkernel()'...\n");
  393. #endif
  394. runkernel();
  395. }
  396. /* dummy function, should never be called. */
  397. void *__kmalloc(size_t size, gfp_t flags)
  398. {
  399. return (void *)NULL;
  400. }