Skip to content

Commit 61cea98

Browse files
authored
Merge pull request #25 from shengwen-tw/master
Implement the loading of initrd image
2 parents 0c43584 + d11f1f6 commit 61cea98

File tree

8 files changed

+41
-17
lines changed

8 files changed

+41
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ semu
77
*.dtb
88
Image
99
ext4.img
10+
rootfs.cpio

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ ext4.img:
8282

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

8787
build-image:
8888
scripts/build-image.sh

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,12 @@ $ make build-image
7878
## Usage
7979

8080
```
81-
./semu -k linux-image [-b dtb-file] [-d disk-image]
81+
./semu -k linux-image [-b dtb-file] [-i initrd-image] [-d disk-image]
8282
```
8383

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

8889
## License

configs/linux.config

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,14 @@ CONFIG_CC_NO_ARRAY_BOUNDS=y
123123
# CONFIG_SYSFS_DEPRECATED is not set
124124
# CONFIG_RELAY is not set
125125
CONFIG_BLK_DEV_INITRD=y
126-
CONFIG_INITRAMFS_SOURCE="../buildroot/output/images/rootfs.cpio"
127-
CONFIG_INITRAMFS_ROOT_UID=0
128-
CONFIG_INITRAMFS_ROOT_GID=0
126+
CONFIG_INITRAMFS_SOURCE=""
129127
# CONFIG_RD_GZIP is not set
130128
# CONFIG_RD_BZIP2 is not set
131129
# CONFIG_RD_LZMA is not set
132130
# CONFIG_RD_XZ is not set
133131
# CONFIG_RD_LZO is not set
134132
# CONFIG_RD_LZ4 is not set
135133
CONFIG_RD_ZSTD=y
136-
CONFIG_INITRAMFS_COMPRESSION_ZSTD=y
137-
# CONFIG_INITRAMFS_COMPRESSION_NONE is not set
138134
# CONFIG_BOOT_CONFIG is not set
139135
CONFIG_INITRAMFS_PRESERVE_MTIME=y
140136
# CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE is not set

device.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
/* RAM */
77

88
#define RAM_SIZE (512 * 1024 * 1024)
9+
#define DTB_SIZE (1 * 1024 * 1024)
10+
#define INITRD_SIZE (8 * 1024 * 1024)
911

1012
void ram_read(vm_t *core,
1113
uint32_t *mem,

main.c

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -291,35 +291,40 @@ static void map_file(char **ram_loc, const char *name)
291291

292292
static void usage(const char *execpath)
293293
{
294-
fprintf(stderr, "Usage: %s -k linux-image [-b dtb] [-d disk-image]\n",
295-
execpath);
294+
fprintf(
295+
stderr,
296+
"Usage: %s -k linux-image [-b dtb] [-i initrd-image] [-d disk-image]\n",
297+
execpath);
296298
}
297299

298300
static void handle_options(int argc,
299301
char **argv,
300302
char **kernel_file,
301303
char **dtb_file,
304+
char **initrd_file,
302305
char **disk_file)
303306
{
304-
*kernel_file = *dtb_file = *disk_file = NULL;
307+
*kernel_file = *dtb_file = *initrd_file = *disk_file = NULL;
305308

306309
int optidx = 0;
307310
struct option opts[] = {
308-
{"kernel", 1, NULL, 'k'},
309-
{"dtb", 1, NULL, 'b'},
310-
{"disk", 1, NULL, 'd'},
311+
{"kernel", 1, NULL, 'k'}, {"dtb", 1, NULL, 'b'},
312+
{"initrd", 1, NULL, 'i'}, {"disk", 1, NULL, 'd'},
311313
{"help", 0, NULL, 'h'},
312314
};
313315

314316
int c;
315-
while ((c = getopt_long(argc, argv, "k:b:d:h", opts, &optidx)) != -1) {
317+
while ((c = getopt_long(argc, argv, "k:b:i:d:h", opts, &optidx)) != -1) {
316318
switch (c) {
317319
case 'k':
318320
*kernel_file = optarg;
319321
break;
320322
case 'b':
321323
*dtb_file = optarg;
322324
break;
325+
case 'i':
326+
*initrd_file = optarg;
327+
break;
323328
case 'd':
324329
*disk_file = optarg;
325330
break;
@@ -347,8 +352,10 @@ static int semu_start(int argc, char **argv)
347352
{
348353
char *kernel_file;
349354
char *dtb_file;
355+
char *initrd_file;
350356
char *disk_file;
351-
handle_options(argc, argv, &kernel_file, &dtb_file, &disk_file);
357+
handle_options(argc, argv, &kernel_file, &dtb_file, &initrd_file,
358+
&disk_file);
352359

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

381+
/* *-----------------------------------------*
382+
* | Memory layout |
383+
* *----------------*----------------*-------*
384+
* | kernel image | initrd image | dtb |
385+
* *----------------*----------------*-------*
386+
*/
374387
char *ram_loc = (char *) emu.ram;
375388
/* Load Linux kernel image */
376389
map_file(&ram_loc, kernel_file);
377-
/* Load at last 1 MiB to prevent kernel / initrd from overwriting it */
378-
uint32_t dtb_addr = RAM_SIZE - 1024 * 1024; /* Device tree */
390+
/* Load at last 1 MiB to prevent kernel from overwriting it */
391+
uint32_t dtb_addr = RAM_SIZE - DTB_SIZE; /* Device tree */
379392
ram_loc = ((char *) emu.ram) + dtb_addr;
380393
map_file(&ram_loc, dtb_file);
394+
/* Load optional initrd image at last 8 MiB before the dtb region to
395+
* prevent kernel from overwritting it
396+
*/
397+
if (initrd_file) {
398+
uint32_t initrd_addr = dtb_addr - INITRD_SIZE; /* Init RAM disk */
399+
ram_loc = ((char *) emu.ram) + initrd_addr;
400+
map_file(&ram_loc, initrd_file);
401+
}
381402

382403
/* Hook for unmapping files */
383404
atexit(unmap_files);

minimal.dts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
chosen {
1313
bootargs = "earlycon console=ttyS0";
1414
stdout-path = "serial0";
15+
linux,initrd-start = <0x1f700000>; /* @403 MiB (503 * 1024 * 1024) */
16+
linux,initrd-end = <0x1fefffff>; /* @511 MiB (511 * 1024 * 1024 - 1) */
1517
};
1618

1719
cpus {

scripts/build-image.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function do_buildroot
3232
ASSERT make olddefconfig
3333
ASSERT make $PARALLEL
3434
popd
35+
cp -f buildroot/output/images/rootfs.cpio ./
3536
}
3637

3738
function do_linux

0 commit comments

Comments
 (0)