**/serial_monitor/serial_monitor
**/mkrootfs/mkrootfs
initrd
+initfs
rootfs
#include <unistd.h>
int main() {
- printf("hello world\n");
+ while(1) {
+ asm("nop;");
+ }
+ //printf("hello world\n");
return 0;
}
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;
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;
// Add %u Support Sun, 06 Jul 2014 12:07:54
// Add %o Support Tue, 08 Oct 2024 22:54:36
// ========================================================================
-#include <assert.h>
#include <types.h>
#include "string.h"
for (i = bgn; i >= 0; i -= 3) {
ch = (n >> i) & 0x07;
- assert(ch >= 0);
- assert(ch <= 7);
-
ch += '0';
if (ch != '0') {
fi
-# 检查smkrootfs命令是否存在
+# 检查mkrootfs命令是否存在
# 若不存在,需要进scripts/mkrootfs手动编译一个
if ! type mkrootfs >/dev/null 2>&1; then
echo "mkrootfs command not found."
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
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;
// 保留管理所有空闲内存的bitmap所占用的内存
reserve_bootmem_bitmap();
+ // 保留rootfs的内存
+ // TODO: 在加载进内核后释放这部分空间
+ reserve_bootmem_boot_module();
+
// 强制保留最开始的一页
// 免得alloc的时候分不清是失败,还是分配的第0页
reserve_bootmem(0, 1);
}
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,
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)
// 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 {
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