GRUB-1.98 changes
[grub2/jjazz.git] / loader / ieee1275 / multiboot2.c
blobfda62fc4b2cf9567ea65fa2868688b505a57e02f
1 /* multiboot.c - boot a multiboot 2 OS image. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2007 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <grub/loader.h>
21 #include <grub/ieee1275/ieee1275.h>
22 #include <grub/multiboot2.h>
23 #include <multiboot2.h>
24 #include <grub/err.h>
25 #include <grub/elf.h>
26 #include <grub/misc.h>
27 #include <grub/mm.h>
28 #include <grub/machine/kernel.h>
29 #include <grub/machine/loader.h>
30 #ifdef __i386__
31 #include <grub/cpu/multiboot.h>
32 #endif
34 typedef void (*kernel_entry_t) (unsigned long, void *, int (void *),
35 unsigned long, unsigned long);
37 /* Claim the memory occupied by the multiboot kernel. */
38 grub_err_t
39 grub_mb2_arch_elf32_hook (Elf32_Phdr *phdr, UNUSED grub_addr_t *addr,
40 int *do_load)
42 int rc;
44 if (phdr->p_type != PT_LOAD)
46 *do_load = 0;
47 return 0;
49 *do_load = 1;
51 rc = grub_claimmap (phdr->p_paddr, phdr->p_memsz);
52 if (rc)
53 return grub_error(GRUB_ERR_OUT_OF_MEMORY, "Couldn't claim %x - %x",
54 phdr->p_paddr, phdr->p_paddr + phdr->p_memsz);
56 grub_dprintf ("loader", "Loading segment at 0x%x - 0x%x\n", phdr->p_paddr,
57 phdr->p_paddr + phdr->p_memsz);
59 return GRUB_ERR_NONE;
62 /* Claim the memory occupied by the multiboot kernel. */
63 grub_err_t
64 grub_mb2_arch_elf64_hook (Elf64_Phdr *phdr, UNUSED grub_addr_t *addr,
65 int *do_load)
67 int rc;
69 if (phdr->p_type != PT_LOAD)
71 *do_load = 0;
72 return 0;
74 *do_load = 1;
76 rc = grub_claimmap (phdr->p_paddr, phdr->p_memsz);
77 if (rc)
78 return grub_error(GRUB_ERR_OUT_OF_MEMORY, "Couldn't claim 0x%lx - 0x%lx",
79 phdr->p_paddr, phdr->p_paddr + phdr->p_memsz);
81 grub_dprintf ("loader", "Loading segment at 0x%lx - 0x%lx\n",
82 (unsigned long) phdr->p_paddr,
83 (unsigned long) (phdr->p_paddr + phdr->p_memsz));
85 return GRUB_ERR_NONE;
88 grub_err_t
89 grub_mb2_arch_module_alloc (grub_size_t size, grub_addr_t *addr)
91 int rc;
93 /* XXX Will need to map on some firmwares. */
94 rc = grub_ieee1275_claim (0, size, MULTIBOOT2_MOD_ALIGN, addr);
95 if (rc)
96 return grub_error (GRUB_ERR_OUT_OF_MEMORY,
97 "Firmware couldn't allocate memory (size 0x%lx)", size);
99 return GRUB_ERR_NONE;
102 grub_err_t
103 grub_mb2_arch_module_free (grub_addr_t addr, grub_size_t size)
105 grub_ieee1275_release (addr, size);
106 return GRUB_ERR_NONE;
109 grub_err_t
110 grub_mb2_tags_arch_create (void)
112 /* Nothing special. */
113 return GRUB_ERR_NONE;
116 /* Release the memory we claimed from Open Firmware above. */
117 void
118 grub_mb2_arch_unload (struct multiboot_tag_header *tags)
120 struct multiboot_tag_header *tag;
122 /* Free all module memory in the tag list. */
123 for_each_tag (tag, tags)
125 if (tag->key == MULTIBOOT2_TAG_MODULE)
127 struct multiboot_tag_module *module =
128 (struct multiboot_tag_module *) tag;
129 grub_ieee1275_release (module->addr, module->size);
134 void
135 grub_mb2_arch_boot (grub_addr_t entry_addr, void *tags)
137 #if defined(__powerpc__)
138 kernel_entry_t entry = (kernel_entry_t) entry_addr;
139 entry (MULTIBOOT2_BOOTLOADER_MAGIC, tags, grub_ieee1275_entry_fn, 0, 0);
140 #elif defined(__i386__)
141 grub_multiboot2_real_boot (entry_addr, tags);
142 #else
143 #error
144 #endif