31 #include "pedigree/kernel/BootstrapInfo.h" 35 extern "C" int main(
int argc,
char *argv[])
44 void *initrd_mapping = MAP_FAILED;
45 void *configdb_mapping = MAP_FAILED;
46 void *kernel_mapping = MAP_FAILED;
47 void *diskimage_mapping = MAP_FAILED;
48 uintptr_t *module_region = (uintptr_t *) MAP_FAILED;
49 size_t initrd_length = 0;
50 size_t configdb_length = 0;
51 size_t kernel_length = 0;
52 size_t diskimage_length = 0;
54 Elf64_Shdr *shdrs = 0;
56 memset(&bs, 0,
sizeof(bs));
60 fprintf(stderr,
"Usage: kernel initrd config_database [diskimage]\n");
64 fprintf(stderr,
"Pedigree is starting...\n");
67 initrd = open(argv[1], O_RDONLY);
70 fprintf(stderr,
"Can't open initrd: %s\n", strerror(errno));
73 configdb = open(argv[2], O_RDONLY);
76 fprintf(stderr,
"Can't open config database: %s\n", strerror(errno));
81 kernel = open(argv[0], O_RDONLY);
84 fprintf(stderr,
"Can't open kernel: %s\n", strerror(errno));
89 r = fstat(initrd, &st);
92 fprintf(stderr,
"Can't stat initrd: %s\n", strerror(errno));
95 initrd_length = st.st_size;
96 initrd_mapping = mmap(0, initrd_length, PROT_READ, MAP_PRIVATE, initrd, 0);
97 if (initrd_mapping == MAP_FAILED)
99 fprintf(stderr,
"Can't map initrd: %s\n", strerror(errno));
102 fprintf(stderr,
"initrd is at %p\n", initrd_mapping);
104 r = fstat(configdb, &st);
107 fprintf(stderr,
"Can't stat config database: %s\n", strerror(errno));
110 configdb_length = st.st_size;
112 mmap(0, configdb_length, PROT_READ, MAP_PRIVATE, configdb, 0);
113 if (configdb_mapping == MAP_FAILED)
115 fprintf(stderr,
"Can't map config database: %s\n", strerror(errno));
118 fprintf(stderr,
"configuration database is at %p\n", configdb_mapping);
120 r = fstat(kernel, &st);
123 fprintf(stderr,
"Can't stat kernel: %s\n", strerror(errno));
126 kernel_length = st.st_size;
128 mmap(0, kernel_length, PROT_READ | PROT_WRITE, MAP_PRIVATE, kernel, 0);
129 if (kernel_mapping == MAP_FAILED)
131 fprintf(stderr,
"Can't map kernel: %s\n", strerror(errno));
134 fprintf(stderr,
"kernel is at %p\n", kernel_mapping);
137 module_region = (uintptr_t *) mmap(
138 0, 0x1000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
139 if (module_region == MAP_FAILED)
142 stderr,
"Can't map module information region: %s\n",
146 fprintf(stderr,
"module region is at %p\n", module_region);
147 memset(module_region, 0, 0x1000);
150 module_region[0] =
reinterpret_cast<uintptr_t
>(initrd_mapping);
152 reinterpret_cast<uintptr_t
>(initrd_mapping) + initrd_length;
155 module_region[4] =
reinterpret_cast<uintptr_t
>(configdb_mapping);
157 reinterpret_cast<uintptr_t
>(configdb_mapping) + configdb_length;
159 bs.mods_addr =
reinterpret_cast<uintptr_t
>(module_region);
164 diskimage = open(argv[3], O_RDWR);
167 fprintf(stderr,
"Can't open disk image: %s\n", strerror(errno));
171 r = fstat(diskimage, &st);
174 fprintf(stderr,
"Can't stat disk image: %s\n", strerror(errno));
178 diskimage_length = st.st_size;
179 diskimage_mapping = mmap(
180 0, diskimage_length, PROT_READ | PROT_WRITE,
181 MAP_PRIVATE | MAP_NORESERVE, diskimage, 0);
182 if (diskimage_mapping == MAP_FAILED)
184 fprintf(stderr,
"Can't map disk image: %s\n", strerror(errno));
187 fprintf(stderr,
"disk image is at %p\n", diskimage_mapping);
191 module_region[8] =
reinterpret_cast<uintptr_t
>(diskimage_mapping);
193 reinterpret_cast<uintptr_t
>(diskimage_mapping) + diskimage_length;
197 ehdr =
reinterpret_cast<Elf64_Ehdr *
>(kernel_mapping);
198 bs.shndx = ehdr->e_shstrndx;
199 bs.num = ehdr->e_shnum;
200 bs.size = ehdr->e_shentsize;
201 bs.addr =
reinterpret_cast<uintptr_t
>(kernel_mapping) + ehdr->e_shoff;
204 shdrs = reinterpret_cast<Elf64_Shdr *>(bs.addr);
205 for (uint32_t i = 1; i < bs.num; ++i)
207 if (shdrs[i].sh_addr)
211 shdrs[i].sh_offset +
reinterpret_cast<uintptr_t
>(kernel_mapping);
215 bs.flags |= MULTIBOOT_FLAG_MODS | MULTIBOOT_FLAG_ELF;
222 if (module_region != MAP_FAILED)
223 munmap(module_region, 0x1000);
224 if (diskimage_mapping != MAP_FAILED)
225 munmap(diskimage_mapping, diskimage_length);
226 if (kernel_mapping != MAP_FAILED)
227 munmap(kernel_mapping, kernel_length);
228 if (configdb_mapping != MAP_FAILED)
229 munmap(configdb_mapping, kernel_length);
230 if (initrd_mapping != MAP_FAILED)
231 munmap(initrd_mapping, initrd_length);
Bootstrap structure passed to the kernel entry point.