Skip to content

Commit 8dbe7bc

Browse files
compress symfile
1 parent df3988c commit 8dbe7bc

File tree

4 files changed

+60
-29
lines changed

4 files changed

+60
-29
lines changed

cmake/custom_targets.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ function(symbols TARGETFILE SOURCEFILE)
177177
set(FILENAME "${CMAKE_BINARY_DIR}/iso/kernel.bin")
178178
set(OUTNAME "${CMAKE_BINARY_DIR}/iso/${TARGETFILE}")
179179
add_custom_command(OUTPUT ${OUTNAME}
180-
COMMAND /bin/nm -a "${CMAKE_BINARY_DIR}/iso/kernel.bin" | sort -d > "${CMAKE_BINARY_DIR}/iso/kernel.sym"
180+
COMMAND /bin/nm -a "${CMAKE_BINARY_DIR}/iso/kernel.bin" | sort -d | gzip -c > "${CMAKE_BINARY_DIR}/iso/kernel.sym"
181181
DEPENDS ${FILENAME})
182182
add_custom_target(SYMBOLS ALL DEPENDS ${OUTNAME})
183183
add_dependencies(SYMBOLS "kernel.bin")

include/ramdisk.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,25 @@ const char* init_ramdisk_from_storage(const char* storage);
5656
*/
5757
const char* init_ramdisk_from_memory(uint8_t* memory, size_t blocks, size_t blocksize);
5858

59+
/**
60+
* @brief Decompress a gzip-compressed memory buffer.
61+
*
62+
* Reads a gzip stream from @p compressed_image of length @p compressed_size
63+
* and produces a newly allocated buffer containing the uncompressed data.
64+
* On success, @p out and @p out_size are set to the buffer pointer and its
65+
* length in bytes.
66+
*
67+
* @param compressed_image Pointer to gzip-compressed input data.
68+
* @param compressed_size Size of the compressed input in bytes.
69+
* @param out Address of a pointer updated to the decompressed buffer.
70+
* @param out_size Address of a variable updated with the decompressed size.
71+
*
72+
* @return true if decompression succeeds, false on error.
73+
*
74+
* @note The caller is responsible for freeing the buffer assigned to @p out.
75+
*/
76+
bool decompress_gzip(uint8_t *compressed_image, size_t compressed_size, uint8_t** out, uint32_t* out_size);
77+
5978
/**
6079
* @brief Inflate and mount the initial ramdisk supplied by the bootloader
6180
*

src/block/ramdisk.c

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -219,33 +219,29 @@ static void zlib_free(voidpf opaque, voidpf addr) {
219219
}
220220
}
221221

222-
bool mount_initial_ramdisk(uint8_t *compressed_image, size_t compressed_size) {
222+
bool decompress_gzip(uint8_t *compressed_image, size_t compressed_size, uint8_t** out, uint32_t* out_size) {
223223
if (compressed_image == NULL) {
224-
preboot_fail("Invalid ramdisk argument");
224+
return false;
225225
}
226226

227227
if (compressed_size < 18) {
228-
preboot_fail("Compressed ramdisk is too small");
228+
return false;
229229
}
230230

231231
if (!(compressed_image[0] == 0x1F && compressed_image[1] == 0x8B && compressed_image[2] == 0x08)) {
232-
preboot_fail("Compressed ramdisk is not a gzip stream");
232+
return false;
233233
}
234234

235235
const uint8_t *tail = compressed_image + compressed_size - 4;
236-
const uint32_t isize =
237-
(uint32_t) tail[0] |
238-
((uint32_t) tail[1] << 8) |
239-
((uint32_t) tail[2] << 16) |
240-
((uint32_t) tail[3] << 24);
236+
*out_size = (uint32_t) tail[0] | ((uint32_t) tail[1] << 8) | ((uint32_t) tail[2] << 16) | ((uint32_t) tail[3] << 24);
241237

242-
if (isize == 0) {
243-
preboot_fail("Compressed ramdisk uncompressed size is zero (corrupt gzip?)");
238+
if (*out_size == 0) {
239+
return false;
244240
}
245241

246-
void *out = kmalloc((size_t) isize);
247-
if (out == NULL) {
248-
preboot_fail("Out of memory allocating ramdisk buffer");
242+
*out = kmalloc(*out_size);
243+
if (*out == NULL) {
244+
return false;
249245
}
250246

251247
z_stream zs;
@@ -255,34 +251,43 @@ bool mount_initial_ramdisk(uint8_t *compressed_image, size_t compressed_size) {
255251
zs.opaque = Z_NULL;
256252
zs.next_in = (Bytef *) compressed_image;
257253
zs.avail_in = (uInt) compressed_size;
258-
zs.next_out = (Bytef *) out;
259-
zs.avail_out = (uInt) isize;
254+
zs.next_out = (Bytef *) *out;
255+
zs.avail_out = (uInt) *out_size;
260256

261257
int rc = inflateInit2(&zs, 16 + MAX_WBITS);
262258
if (rc != Z_OK) {
263-
preboot_fail("inflateInit2 failed when extracting initial ramdisk");
259+
kfree(*out);
260+
return false;
264261
}
265-
266262
rc = inflate(&zs, Z_FINISH);
267263
if (rc != Z_STREAM_END) {
268264
inflateEnd(&zs);
269-
preboot_fail("inflate failed when extracting initial ramdisk");
265+
kfree(*out);
266+
return false;
270267
}
271-
272268
const size_t out_len = (size_t) zs.total_out;
273269
inflateEnd(&zs);
274-
275270
if (out_len == 0) {
276-
preboot_fail("Decompressed ramdisk length is zero");
271+
kfree(*out);
272+
return false;
273+
}
274+
return true;
275+
}
276+
277+
bool mount_initial_ramdisk(uint8_t *compressed_image, size_t compressed_size) {
278+
279+
uint32_t isize;
280+
uint8_t* out;
281+
if (!decompress_gzip(compressed_image, compressed_size, &out, &isize)) {
282+
preboot_fail("Failed to decompress initial ramdisk");
277283
}
278284

279285
const size_t block_size = 2048;
280-
if ((out_len % block_size) != 0) {
286+
if ((isize % block_size) != 0) {
281287
preboot_fail("Ramdisk is not an integer multiple of 2048 bytes");
282288
}
283289

284-
const size_t blocks = out_len / block_size;
285-
290+
const size_t blocks = isize / block_size;
286291
const char *rd_name = init_ramdisk_from_memory((uint8_t *) out, blocks, block_size);
287292
if (rd_name == NULL) {
288293
preboot_fail("Failed to register initial ramdisk device");

src/debugger.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,19 @@ bool find_limine_module(const char* name, uint8_t** content, size_t* size) {
9393

9494
void init_debug()
9595
{
96+
unsigned char* c_ptr = NULL;
97+
size_t c_filesize = 0;
9698
unsigned char* ptr = NULL;
97-
size_t filesize = 0;
98-
if (!find_limine_module("/kernel.sym", &ptr, &filesize)) {
99+
uint32_t filesize = 0;
100+
if (!find_limine_module("/kernel.sym", &c_ptr, &c_filesize)) {
99101
symbol_fail();
100102
return;
101103
}
104+
if (!decompress_gzip(c_ptr, c_filesize, &ptr, &filesize)) {
105+
symbol_fail();
106+
return;
107+
}
108+
102109
char symbol_address[32];
103110
char type[2];
104111
char symbol[1024];
@@ -169,7 +176,7 @@ void init_debug()
169176
setforeground(COLOUR_LIGHTYELLOW);
170177
kprintf("/kernel.sym ");
171178
setforeground(COLOUR_WHITE);
172-
kprintf("(%ld bytes)\n", filesize);
179+
kprintf("(%d bytes)\n", filesize);
173180
}
174181

175182
const char* findsymbol(uint64_t address, uint64_t* offset) {

0 commit comments

Comments
 (0)