void loader_main(struct multiboot_info *mb) { multiboot_module_t *mod; multiboot_memory_map_t *mmap; uint32_t kernel_entry, kernel_end; uint64_t max_addr = 0; uint64_t max_avail = 0; int m = 0; serial_init(); printk("Memory map from multiboot info:\n"); for (mmap = (multiboot_memory_map_t *)mb->mmap_addr; (uint32_t)mmap < mb->mmap_addr + mb->mmap_length; mmap++) { m += mmap->len; printk("%d 0x%llx - 0x%llx [%d] (%d)\n", mmap->size, mmap->addr, mmap->addr + mmap->len, m/1024, mmap->type); max_addr = mmap->addr + mmap->len; if ( mmap->type == MULTIBOOT_MEMORY_AVAILABLE ) max_avail = max_addr; } mod = (multiboot_module_t *) mb->mods_addr; printk("Found: %s\n", (char *)mod->cmdline); /* update mod_end with room for bss... we really should find the bss and fill it with zeros so the kernel doesn't need to worry about doing the zeroing. */ mod->mod_end = elf_get_end(mod->mod_start); kernel_entry = elf_get_entry(mod->mod_start); printk("mod_start: 0x%lx\n", mod->mod_start); printk("mod_end: 0x%lx\n", mod->mod_end); printk("max_addr: 0x%llx\n", max_addr); printk("max_avail: 0x%llx\n", max_avail); kernel_end = ((mod->mod_end & PAGE_MASK) + PAGE_SIZE); pagetable_init(max_addr, kernel_end); gdt_init(); to64_jump(kernel_entry, (uint32_t)mb, max_avail - 8); }
int main(int argc, char *argv[]) { unsigned long entry, offset, len, key_offset, key_len; unsigned char *buf; struct stat sb; int fd; if (argc < 6) { printf("Usage: %s filename offset len key_offset key_len\n" " filename: file to encrypt\n" " offset: offset in file to start encryption\n" " len: number of bytes to encrypt\n" " key_offset: offset in file of key\n" " key_len: number of bytes in key\n" " Values are converted with strtol with base 0\n", argv[0]); return 1; } /* prepare the parameters */ offset = strtoul(argv[2], 0, 0); len = strtoul(argv[3], 0, 0); key_offset = strtoul(argv[4], 0, 0); key_len = strtoul(argv[5], NULL, 0); /* memory map the file so we can access it via pointers */ if (stat(argv[1], &sb)) { fprintf(stderr, "Stat failed: %s\n", strerror(errno)); return 2; } if ((fd = open(argv[1], O_RDWR | O_EXCL)) < 0) { fprintf(stderr, "Open failed: %s\n", strerror(errno)); return 3; } buf = static_cast<unsigned char*> (mmap(0, sb.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)); if ((long) buf < 0) { fprintf(stderr, "Open failed: %s\n", strerror(errno)); close(fd); return 4; } /* get entry point : here we assume ELF example */ entry = elf_get_entry(buf); if (!entry) { fprintf(stderr, "Invalid ELF header\n"); munmap(buf, sb.st_size); close(fd); return 5; } /* these are offsets from the entry point */ offset += entry; key_offset += entry; printf("Encrypting %ld bytes at 0x%lX with %ld bytes at 0x%lX\n", len, offset, key_len, key_offset); /* Because we're using RC4, encryption and decryption are the same operation */ spc_smc_decrypt(buf + offset, len, buf + key_offset, key_len); /* mem-unmap the file */ msync(buf, sb.st_size, MS_SYNC); munmap(buf, sb.st_size); close(fd); return 0; }