@@ -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" );
0 commit comments