Support compressed initramfs image

This commit is contained in:
LI Qing 2023-06-19 16:28:14 +08:00 committed by Tate, Hongliang Tian
parent fba4a9405e
commit d692c102ae
7 changed files with 78 additions and 6 deletions

53
Cargo.lock generated
View File

@ -13,6 +13,12 @@ dependencies = [
"rsdp",
]
[[package]]
name = "adler32"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234"
[[package]]
name = "ahash"
version = "0.8.2"
@ -156,6 +162,15 @@ dependencies = [
"syn 1.0.109",
]
[[package]]
name = "core2"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b49ba7ef1ad6107f8824dbe97de947cbaac53c44e7f9756a1fba0d37c1eec505"
dependencies = [
"memchr",
]
[[package]]
name = "cpio-decoder"
version = "0.1.0"
@ -163,6 +178,15 @@ dependencies = [
"int-to-c-enum",
]
[[package]]
name = "crc32fast"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d"
dependencies = [
"cfg-if",
]
[[package]]
name = "critical-section"
version = "1.1.1"
@ -538,6 +562,7 @@ dependencies = [
"ascii",
"bitflags",
"controlled",
"core2",
"cpio-decoder",
"int-to-c-enum",
"intrusive-collections",
@ -551,6 +576,7 @@ dependencies = [
"jinux-util",
"keyable-arc",
"lazy_static",
"libflate",
"log",
"lru",
"pod",
@ -628,6 +654,27 @@ version = "0.2.137"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89"
[[package]]
name = "libflate"
version = "1.4.0"
source = "git+https://github.com/jinzhao-dev/libflate?rev=b781da6#b781da6b6841e380f4cfa3529d5070afad56ea32"
dependencies = [
"adler32",
"core2",
"crc32fast",
"libflate_lz77",
]
[[package]]
name = "libflate_lz77"
version = "1.2.0"
source = "git+https://github.com/jinzhao-dev/libflate?rev=b781da6#b781da6b6841e380f4cfa3529d5070afad56ea32"
dependencies = [
"core2",
"hashbrown 0.13.1",
"rle-decode-fast",
]
[[package]]
name = "limine"
version = "0.1.10"
@ -776,6 +823,12 @@ dependencies = [
"crossbeam-utils",
]
[[package]]
name = "rle-decode-fast"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3582f63211428f83597b51b2ddb88e2a91a9d52d12831f9d08f5e624e8977422"
[[package]]
name = "rsdp"
version = "2.0.0"

View File

@ -15,4 +15,4 @@ PROTOCOL=limine
# so change this path if you change that.
KERNEL_PATH=boot:///jinux
# The path to the module
MODULE_PATH=boot:///ramdisk.cpio
MODULE_PATH=boot:///ramdisk.cpio.gz

View File

@ -25,7 +25,7 @@ cp target/limine/limine-cd.bin target/iso_root
cp target/limine/limine-cd-efi.bin target/iso_root
# Copy ramdisk
cp regression/build/ramdisk.cpio target/iso_root
cp regression/build/ramdisk.cpio.gz target/iso_root
xorriso -as mkisofs \
-b limine-cd.bin \

View File

@ -4,7 +4,7 @@ use log::Level;
pub const USER_STACK_SIZE: usize = PAGE_SIZE * 4;
pub const KERNEL_STACK_SIZE: usize = PAGE_SIZE * 64;
pub const KERNEL_HEAP_SIZE: usize = 0x2_000_000;
pub const KERNEL_HEAP_SIZE: usize = 0x4_000_000;
pub const KERNEL_OFFSET: usize = 0xffffffff80000000;

View File

@ -2,7 +2,7 @@ MKFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
CUR_DIR := $(patsubst %/,%,$(dir $(MKFILE_PATH)))
BUILD_DIR := $(CUR_DIR)/build
INITRAMFS := $(BUILD_DIR)/initramfs
RAMDISK := $(BUILD_DIR)/ramdisk.cpio
RAMDISK := $(BUILD_DIR)/ramdisk.cpio.gz
SHELL := /bin/bash
ifneq (, $(wildcard $(INITRAMFS)/. ))
@ -37,7 +37,7 @@ endif
$(RAMDISK): $(INITRAMFS) $(INITRAMFS_DIRS) $(INITRAMFS_FILES)
@echo "Generating the ramdisk image..."
@(cd $(INITRAMFS); find . | cpio -o -H newc) > $@
@(cd $(INITRAMFS); find . | cpio -o -H newc | gzip) > $@
build: $(RAMDISK)

View File

@ -34,6 +34,9 @@ xmas-elf = "0.8.0"
bitflags = "1.3"
ringbuf = { version = "0.3.2", default-features = false, features = ["alloc"] }
keyable-arc = { path = "../keyable-arc" }
# unzip initramfs
libflate = { git = "https://github.com/jinzhao-dev/libflate", rev = "b781da6", features = ["no_std"] }
core2 = { version = "0.4", default_features = false, features = ["alloc"] }
spin = "0.9.4"
vte = "0.10"

View File

@ -2,11 +2,16 @@ use crate::prelude::*;
use super::fs_resolver::{FsPath, FsResolver};
use super::utils::{InodeMode, InodeType};
use core2::io::Read;
use cpio_decoder::{CpioDecoder, FileType};
use libflate::gzip::Decoder as GZipDecoder;
/// Unpack and prepare the fs from the ramdisk CPIO buffer.
pub fn init(ramdisk_buf: &[u8]) -> Result<()> {
let decoder = CpioDecoder::new(ramdisk_buf);
println!("[kernel] unzipping ramdisk.cpio.gz ...");
let unzipped_ramdisk_buf = unzip(ramdisk_buf)?;
println!("[kernel] unzip ramdisk.cpio.gz done");
let decoder = CpioDecoder::new(&unzipped_ramdisk_buf);
let fs = FsResolver::new();
for entry_result in decoder.decode_entries() {
let entry = entry_result?;
@ -50,6 +55,17 @@ pub fn init(ramdisk_buf: &[u8]) -> Result<()> {
}
}
}
println!("[kernel] initramfs is ready");
Ok(())
}
fn unzip(buf: &[u8]) -> Result<Vec<u8>> {
let mut decoder = GZipDecoder::new(buf)
.map_err(|_| Error::with_message(Errno::EINVAL, "invalid gzip buffer"))?;
let mut unzipped_buf = Vec::new();
decoder
.read_to_end(&mut unzipped_buf)
.map_err(|_| Error::with_message(Errno::EINVAL, "invalid gzip buffer"))?;
Ok(unzipped_buf)
}