Skip to content

Commit d97c6a3

Browse files
committed
[bzimage] Allow file mode to be specified for standalone initrd files
Allow the file mode to be specified using a "mode=" command line parameter. For example: initrd http://web/boot/bootlocal.sh /opt/bootlocal.sh mode=755 Requested-by: Bryce Zimmerman <bryce.zimmerman@gmail.com> Signed-off-by: Michael Brown <mcb30@ipxe.org>
1 parent 1f88e9c commit d97c6a3

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

src/arch/i386/image/bzimage.c

+37-3
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,32 @@ static int bzimage_set_cmdline ( struct image *image,
322322
return 0;
323323
}
324324

325+
/**
326+
* Parse standalone image command line for cpio parameters
327+
*
328+
* @v image bzImage file
329+
* @v cpio CPIO header
330+
* @v cmdline Command line
331+
*/
332+
static void bzimage_parse_cpio_cmdline ( struct image *image,
333+
struct cpio_header *cpio,
334+
const char *cmdline ) {
335+
char *arg;
336+
char *end;
337+
unsigned int mode;
338+
339+
/* Look for "mode=" */
340+
if ( ( arg = strstr ( cmdline, "mode=" ) ) ) {
341+
arg += 5;
342+
mode = strtoul ( arg, &end, 8 /* Octal for file mode */ );
343+
if ( *end && ( *end != ' ' ) ) {
344+
DBGC ( image, "bzImage %p strange \"mode=\""
345+
"terminator '%c'\n", image, *end );
346+
}
347+
cpio_set_field ( cpio->c_mode, ( 0100000 | mode ) );
348+
}
349+
}
350+
325351
/**
326352
* Load initrd
327353
*
@@ -334,33 +360,41 @@ static size_t bzimage_load_initrd ( struct image *image,
334360
struct image *initrd,
335361
userptr_t address ) {
336362
char *filename = initrd->cmdline;
363+
char *cmdline;
337364
struct cpio_header cpio;
338365
size_t offset = 0;
366+
size_t name_len;
339367

340368
/* Do not include kernel image itself as an initrd */
341369
if ( initrd == image )
342370
return 0;
343371

344372
/* Create cpio header before non-prebuilt images */
345373
if ( filename && filename[0] ) {
346-
size_t name_len = ( strlen ( filename ) + 1 );
347-
348374
DBGC ( image, "bzImage %p inserting initrd %p as %s\n",
349375
image, initrd, filename );
376+
cmdline = strchr ( filename, ' ' );
377+
name_len = ( ( cmdline ? ( ( size_t ) ( cmdline - filename ) )
378+
: strlen ( filename ) ) + 1 /* NUL */ );
350379
memset ( &cpio, '0', sizeof ( cpio ) );
351380
memcpy ( cpio.c_magic, CPIO_MAGIC, sizeof ( cpio.c_magic ) );
352381
cpio_set_field ( cpio.c_mode, 0100644 );
353382
cpio_set_field ( cpio.c_nlink, 1 );
354383
cpio_set_field ( cpio.c_filesize, initrd->len );
355384
cpio_set_field ( cpio.c_namesize, name_len );
385+
if ( cmdline ) {
386+
bzimage_parse_cpio_cmdline ( image, &cpio,
387+
( cmdline + 1 /* ' ' */ ));
388+
}
356389
if ( address ) {
357390
copy_to_user ( address, offset, &cpio,
358391
sizeof ( cpio ) );
359392
}
360393
offset += sizeof ( cpio );
361394
if ( address ) {
395+
memset_user ( address, offset, 0, name_len );
362396
copy_to_user ( address, offset, filename,
363-
name_len );
397+
( name_len - 1 /* NUL (or space) */ ) );
364398
}
365399
offset += name_len;
366400
offset = ( ( offset + 0x03 ) & ~0x03 );

0 commit comments

Comments
 (0)