Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the loading of initrd image #25

Merged
merged 5 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ semu
*.dtb
Image
ext4.img
rootfs.cpio
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ ext4.img:

check: $(BIN) minimal.dtb $(KERNEL_DATA) $(DISKIMG_FILE)
@$(call notice, Ready to launch Linux kernel. Please be patient.)
$(Q)./$(BIN) -k $(KERNEL_DATA) -b minimal.dtb $(OPTS)
$(Q)./$(BIN) -k $(KERNEL_DATA) -b minimal.dtb -i rootfs.cpio $(OPTS)

build-image:
scripts/build-image.sh
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ $ make build-image
## Usage

```
./semu -k linux-image [-b dtb-file] [-d disk-image]
./semu -k linux-image [-b dtb-file] [-i initrd-image] [-d disk-image]
```

* `linux-image` is the path to the Linux kernel `Image`.
* `dtb-file` is optional, as it specifies the user-specified device tree blob.
* `initrd-image` is optional, as it specifies the user-specified init RAM disk image.
* `disk-image` is optional, as it specifies the path of a disk image in ext4 file system for the virtio-blk device.

## License
Expand Down
6 changes: 1 addition & 5 deletions configs/linux.config
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,14 @@ CONFIG_CC_NO_ARRAY_BOUNDS=y
# CONFIG_SYSFS_DEPRECATED is not set
# CONFIG_RELAY is not set
CONFIG_BLK_DEV_INITRD=y
CONFIG_INITRAMFS_SOURCE="../buildroot/output/images/rootfs.cpio"
CONFIG_INITRAMFS_ROOT_UID=0
CONFIG_INITRAMFS_ROOT_GID=0
CONFIG_INITRAMFS_SOURCE=""
# CONFIG_RD_GZIP is not set
# CONFIG_RD_BZIP2 is not set
# CONFIG_RD_LZMA is not set
# CONFIG_RD_XZ is not set
# CONFIG_RD_LZO is not set
# CONFIG_RD_LZ4 is not set
CONFIG_RD_ZSTD=y
CONFIG_INITRAMFS_COMPRESSION_ZSTD=y
# CONFIG_INITRAMFS_COMPRESSION_NONE is not set
# CONFIG_BOOT_CONFIG is not set
CONFIG_INITRAMFS_PRESERVE_MTIME=y
# CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE is not set
Expand Down
2 changes: 2 additions & 0 deletions device.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
/* RAM */

#define RAM_SIZE (512 * 1024 * 1024)
#define DTB_SIZE (1 * 1024 * 1024)
#define INITRD_SIZE (8 * 1024 * 1024)

void ram_read(vm_t *core,
uint32_t *mem,
Expand Down
41 changes: 31 additions & 10 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,35 +291,40 @@ static void map_file(char **ram_loc, const char *name)

static void usage(const char *execpath)
{
fprintf(stderr, "Usage: %s -k linux-image [-b dtb] [-d disk-image]\n",
execpath);
fprintf(
stderr,
"Usage: %s -k linux-image [-b dtb] [-i initrd-image] [-d disk-image]\n",
execpath);
}

static void handle_options(int argc,
char **argv,
char **kernel_file,
char **dtb_file,
char **initrd_file,
char **disk_file)
{
*kernel_file = *dtb_file = *disk_file = NULL;
*kernel_file = *dtb_file = *initrd_file = *disk_file = NULL;

int optidx = 0;
struct option opts[] = {
{"kernel", 1, NULL, 'k'},
{"dtb", 1, NULL, 'b'},
{"disk", 1, NULL, 'd'},
{"kernel", 1, NULL, 'k'}, {"dtb", 1, NULL, 'b'},
{"initrd", 1, NULL, 'i'}, {"disk", 1, NULL, 'd'},
{"help", 0, NULL, 'h'},
};

int c;
while ((c = getopt_long(argc, argv, "k:b:d:h", opts, &optidx)) != -1) {
while ((c = getopt_long(argc, argv, "k:b:i:d:h", opts, &optidx)) != -1) {
switch (c) {
case 'k':
*kernel_file = optarg;
break;
case 'b':
*dtb_file = optarg;
break;
case 'i':
*initrd_file = optarg;
break;
case 'd':
*disk_file = optarg;
break;
Expand Down Expand Up @@ -347,8 +352,10 @@ static int semu_start(int argc, char **argv)
{
char *kernel_file;
char *dtb_file;
char *initrd_file;
char *disk_file;
handle_options(argc, argv, &kernel_file, &dtb_file, &disk_file);
handle_options(argc, argv, &kernel_file, &dtb_file, &initrd_file,
&disk_file);

/* Initialize the emulator */
emu_state_t emu;
Expand All @@ -371,13 +378,27 @@ static int semu_start(int argc, char **argv)
}
assert(!(((uintptr_t) emu.ram) & 0b11));

/* *-----------------------------------------*
* | Memory layout |
* *----------------*----------------*-------*
* | kernel image | initrd image | dtb |
* *----------------*----------------*-------*
*/
char *ram_loc = (char *) emu.ram;
/* Load Linux kernel image */
map_file(&ram_loc, kernel_file);
/* Load at last 1 MiB to prevent kernel / initrd from overwriting it */
uint32_t dtb_addr = RAM_SIZE - 1024 * 1024; /* Device tree */
/* Load at last 1 MiB to prevent kernel from overwriting it */
uint32_t dtb_addr = RAM_SIZE - DTB_SIZE; /* Device tree */
ram_loc = ((char *) emu.ram) + dtb_addr;
map_file(&ram_loc, dtb_file);
/* Load optional initrd image at last 8 MiB before the dtb region to
* prevent kernel from overwritting it
*/
if (initrd_file) {
uint32_t initrd_addr = dtb_addr - INITRD_SIZE; /* Init RAM disk */
ram_loc = ((char *) emu.ram) + initrd_addr;
map_file(&ram_loc, initrd_file);
}

/* Hook for unmapping files */
atexit(unmap_files);
Expand Down
2 changes: 2 additions & 0 deletions minimal.dts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
chosen {
bootargs = "earlycon console=ttyS0";
stdout-path = "serial0";
linux,initrd-start = <0x1f700000>; /* @403 MiB (503 * 1024 * 1024) */
linux,initrd-end = <0x1fefffff>; /* @511 MiB (511 * 1024 * 1024 - 1) */
};

cpus {
Expand Down
1 change: 1 addition & 0 deletions scripts/build-image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function do_buildroot
ASSERT make olddefconfig
ASSERT make $PARALLEL
popd
cp -f buildroot/output/images/rootfs.cpio ./
}

function do_linux
Expand Down