From 7b7ded820a321623e5004417923b0ca74f73e555 Mon Sep 17 00:00:00 2001 From: acevest Date: Thu, 10 Oct 2024 22:07:35 +0800 Subject: [PATCH] =?utf8?q?mkrootfs=E7=9B=B8=E5=85=B3=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + bin/hello.c | 5 ++++- boot/boot.c | 42 ++++++++++++++++++++++++++++++++-------- boot/boot.h | 3 +++ lib/vsprintf.c | 4 ---- mkiso.sh | 5 +++-- mm/bootmem.c | 11 +++++++++++ scripts/mkrootfs/main.go | 32 +++++++++++++++++++++++++----- 8 files changed, 83 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 7e9c83e..518d400 100644 --- a/.gitignore +++ b/.gitignore @@ -41,4 +41,5 @@ bochsout.txt **/serial_monitor/serial_monitor **/mkrootfs/mkrootfs initrd +initfs rootfs diff --git a/bin/hello.c b/bin/hello.c index aecbfc8..a2d5f82 100644 --- a/bin/hello.c +++ b/bin/hello.c @@ -13,7 +13,10 @@ #include int main() { - printf("hello world\n"); + while(1) { + asm("nop;"); + } + //printf("hello world\n"); return 0; } diff --git a/boot/boot.c b/boot/boot.c index 7498796..dd1a0ef 100644 --- a/boot/boot.c +++ b/boot/boot.c @@ -114,15 +114,41 @@ void check_kernel(unsigned long addr, unsigned long magic) { struct multiboot_tag_module *m = (struct multiboot_tag_module *)tag; void *mod_start = (void *)m->mod_start; printk("module 0x%08x - 0x%08x size %u cmdline %s\n", m->mod_start, m->mod_end, m->size, m->cmdline); - // TODO 在操作系统中保留这段内存 - uint32_t *mod_magic = (uint32_t *)(mod_start + 0); - uint32_t *mod_timestamp = (uint32_t *)(mod_start + 8); - uint32_t *mod_file_entry_cnt = (uint32_t *)(mod_start + 12); - char *mod_name = (char *)mod_start + 16; - printk("module magic %08x timestamp %u file entry cnt %u name %s \n", *mod_magic, *mod_timestamp, - *mod_file_entry_cnt, mod_name); + boot_params.boot_module_begin = (void *)m->mod_start; + boot_params.boot_module_end = (void *)m->mod_end; + + const uint32_t mod_magic = *(uint32_t *)(mod_start + 0); + const uint32_t mod_head_size = *(uint32_t *)(mod_start + 4); + const uint32_t mod_timestamp = *(uint32_t *)(mod_start + 8); + const uint32_t mod_file_entry_cnt = *(uint32_t *)(mod_start + 12); + const char *mod_name = (const char *)mod_start + 16; + printk("module magic %08x header size %u timestamp %u file entry cnt %u name %s \n", mod_magic, + mod_head_size, mod_timestamp, mod_file_entry_cnt, mod_name); void timestamp_to_date(uint32_t ts); - timestamp_to_date(*mod_timestamp); + timestamp_to_date(mod_timestamp); + + int file_entry_offset = mod_head_size; + for (int i = 0; i < mod_file_entry_cnt; i++) { + void *fe = mod_start + file_entry_offset; + + const uint32_t fe_size = *(uint32_t *)(fe + 0); + const uint32_t fe_type = *(uint32_t *)(fe + 4); + const uint32_t fe_filesz = *(uint32_t *)(fe + 8); + const uint32_t fe_offset = *(uint32_t *)(fe + 12); + const char *fe_name = (const char *)(fe + 16); + + file_entry_offset += fe_size; + + void *fc = mod_start + fe_offset; + + printk("[fe:%u:%u] file size %u type %u name %s\n", i, fe_size, fe_filesz, fe_type, fe_name); + + for (int k = 0; k < 16; k++) { + uint8_t c = *(uint8_t *)(fc + k); + printk("%02X ", c); + } + printk("\n"); + } break; case MULTIBOOT_TAG_TYPE_BASIC_MEMINFO: mminfo = (struct multiboot_tag_basic_meminfo *)tag; diff --git a/boot/boot.h b/boot/boot.h index 9d5c3b4..d236c8d 100644 --- a/boot/boot.h +++ b/boot/boot.h @@ -44,6 +44,9 @@ struct boot_params { unsigned long mem_lower; // in bytes unsigned long mem_upper; + void *boot_module_begin; + void *boot_module_end; + unsigned long biosdev; unsigned long partition; unsigned long sub_partition; diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 5c53fd5..21488e1 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -5,7 +5,6 @@ // Add %u Support Sun, 06 Jul 2014 12:07:54 // Add %o Support Tue, 08 Oct 2024 22:54:36 // ======================================================================== -#include #include #include "string.h" @@ -198,9 +197,6 @@ char *_itoo(char *s, uint64_t n, int bgn) { for (i = bgn; i >= 0; i -= 3) { ch = (n >> i) & 0x07; - assert(ch >= 0); - assert(ch <= 7); - ch += '0'; if (ch != '0') { diff --git a/mkiso.sh b/mkiso.sh index 0eacfc3..d0fcbc8 100755 --- a/mkiso.sh +++ b/mkiso.sh @@ -14,7 +14,7 @@ if [[ `uname` == 'Darwin' ]]; then fi -# 检查smkrootfs命令是否存在 +# 检查mkrootfs命令是否存在 # 若不存在,需要进scripts/mkrootfs手动编译一个 if ! type mkrootfs >/dev/null 2>&1; then echo "mkrootfs command not found." @@ -33,7 +33,8 @@ fi echo "container id ${CONTAINER_ID}" -mkrootfs -name rootfs -path initrd +mkdir -p initfs +mkrootfs -name rootfs -path initfs grub2_boot_dir="/tmp/iso/boot" docker exec -it $CONTAINER_ID rm -rf /tmp/iso diff --git a/mm/bootmem.c b/mm/bootmem.c index c8d2f52..05facda 100644 --- a/mm/bootmem.c +++ b/mm/bootmem.c @@ -213,6 +213,13 @@ void reserve_bootmem_bitmap() { reserve_bootmem(bgn_pfn, end_pfn); } +void reserve_bootmem_boot_module() { + unsigned long bgn_pfn = PFN_DW((unsigned long)boot_params.boot_module_begin); + unsigned long end_pfn = PFN_UP((unsigned long)boot_params.boot_module_end); + + reserve_bootmem(bgn_pfn, end_pfn); +} + void init_bootmem_allocator() { int mapsize = (bootmem_data.max_pfn + 7) / 8; @@ -230,6 +237,10 @@ void init_bootmem_allocator() { // 保留管理所有空闲内存的bitmap所占用的内存 reserve_bootmem_bitmap(); + // 保留rootfs的内存 + // TODO: 在加载进内核后释放这部分空间 + reserve_bootmem_boot_module(); + // 强制保留最开始的一页 // 免得alloc的时候分不清是失败,还是分配的第0页 reserve_bootmem(0, 1); diff --git a/scripts/mkrootfs/main.go b/scripts/mkrootfs/main.go index 9a413b4..a7d33bb 100644 --- a/scripts/mkrootfs/main.go +++ b/scripts/mkrootfs/main.go @@ -125,9 +125,9 @@ func main() { } path := strings.TrimPrefix(filePath, pathPrefix) - + path += strings.Repeat("\x00", 4-(len(path)%4)) entry := FileEntry{ - EntrySize: uint32(8 + len(path)), + EntrySize: uint32(16 + len(path)), FileType: fileType, FileName: path, FileSize: fileSize, @@ -157,6 +157,7 @@ func main() { hdr.Timestamp = uint32(time.Now().Unix()) hdr.FileEntryCnt = uint32(len(entries)) hdr.Name = outputFileName + "\x00" + hdr.Name += strings.Repeat("\x00", 4-(len(hdr.Name)%4)) hdr.CalculateHeaderSize() binary.Write(outputFile, binary.LittleEndian, hdr.Magic) @@ -186,10 +187,21 @@ func main() { // log.Fatal(err) // } + fileHeaderSize, _ := outputFile.Seek(0, io.SeekCurrent) + fileContentOffset := uint32(fileHeaderSize) + log.Printf("file header size %v\n", fileHeaderSize) + for _, entry := range entries { + fileContentOffset += 4 // EntrySize + fileContentOffset += 4 // FileType + fileContentOffset += 4 // FileSize + fileContentOffset += 4 // FileContentOffset + fileContentOffset += uint32(len(entry.FileName)) + log.Printf("fe %v\n", fileContentOffset) + } + // 写入每个文件项 - currentOffset := uint32(4 + len(entries)*12) for _, entry := range entries { - entry.Offset = currentOffset + entry.Offset = fileContentOffset err = binary.Write(outputFile, binary.LittleEndian, entry.EntrySize) if err != nil { @@ -201,12 +213,22 @@ func main() { log.Fatal(err) } + err = binary.Write(outputFile, binary.LittleEndian, entry.FileSize) + if err != nil { + log.Fatal(err) + } + + err = binary.Write(outputFile, binary.LittleEndian, fileContentOffset) + if err != nil { + log.Fatal(err) + } + _, err = outputFile.WriteString(entry.FileName) if err != nil { log.Fatal(err) } - currentOffset += entry.FileSize + fileContentOffset += entry.FileSize } // 逐个读取文件并追加到 outputFileName -- 2.44.0