Browse Source

[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>
tags/v1.20.1
Michael Brown 12 years ago
parent
commit
d97c6a321e
1 changed files with 37 additions and 3 deletions
  1. 37
    3
      src/arch/i386/image/bzimage.c

+ 37
- 3
src/arch/i386/image/bzimage.c View File

322
 	return 0;
322
 	return 0;
323
 }
323
 }
324
 
324
 
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
+
325
 /**
351
 /**
326
  * Load initrd
352
  * Load initrd
327
  *
353
  *
334
 				    struct image *initrd,
360
 				    struct image *initrd,
335
 				    userptr_t address ) {
361
 				    userptr_t address ) {
336
 	char *filename = initrd->cmdline;
362
 	char *filename = initrd->cmdline;
363
+	char *cmdline;
337
 	struct cpio_header cpio;
364
 	struct cpio_header cpio;
338
         size_t offset = 0;
365
         size_t offset = 0;
366
+	size_t name_len;
339
 
367
 
340
 	/* Do not include kernel image itself as an initrd */
368
 	/* Do not include kernel image itself as an initrd */
341
 	if ( initrd == image )
369
 	if ( initrd == image )
343
 
371
 
344
 	/* Create cpio header before non-prebuilt images */
372
 	/* Create cpio header before non-prebuilt images */
345
 	if ( filename && filename[0] ) {
373
 	if ( filename && filename[0] ) {
346
-		size_t name_len = ( strlen ( filename ) + 1 );
347
-
348
 		DBGC ( image, "bzImage %p inserting initrd %p as %s\n",
374
 		DBGC ( image, "bzImage %p inserting initrd %p as %s\n",
349
 		       image, initrd, filename );
375
 		       image, initrd, filename );
376
+		cmdline = strchr ( filename, ' ' );
377
+		name_len = ( ( cmdline ? ( ( size_t ) ( cmdline - filename ) )
378
+			       : strlen ( filename ) ) + 1 /* NUL */ );
350
 		memset ( &cpio, '0', sizeof ( cpio ) );
379
 		memset ( &cpio, '0', sizeof ( cpio ) );
351
 		memcpy ( cpio.c_magic, CPIO_MAGIC, sizeof ( cpio.c_magic ) );
380
 		memcpy ( cpio.c_magic, CPIO_MAGIC, sizeof ( cpio.c_magic ) );
352
 		cpio_set_field ( cpio.c_mode, 0100644 );
381
 		cpio_set_field ( cpio.c_mode, 0100644 );
353
 		cpio_set_field ( cpio.c_nlink, 1 );
382
 		cpio_set_field ( cpio.c_nlink, 1 );
354
 		cpio_set_field ( cpio.c_filesize, initrd->len );
383
 		cpio_set_field ( cpio.c_filesize, initrd->len );
355
 		cpio_set_field ( cpio.c_namesize, name_len );
384
 		cpio_set_field ( cpio.c_namesize, name_len );
385
+		if ( cmdline ) {
386
+			bzimage_parse_cpio_cmdline ( image, &cpio,
387
+						     ( cmdline + 1 /* ' ' */ ));
388
+		}
356
 		if ( address ) {
389
 		if ( address ) {
357
 			copy_to_user ( address, offset, &cpio,
390
 			copy_to_user ( address, offset, &cpio,
358
 				       sizeof ( cpio ) );
391
 				       sizeof ( cpio ) );
359
 		}
392
 		}
360
 		offset += sizeof ( cpio );
393
 		offset += sizeof ( cpio );
361
 		if ( address ) {
394
 		if ( address ) {
395
+			memset_user ( address, offset, 0, name_len );
362
 			copy_to_user ( address, offset, filename,
396
 			copy_to_user ( address, offset, filename,
363
-				       name_len );
397
+				       ( name_len - 1 /* NUL (or space) */ ) );
364
 		}
398
 		}
365
 		offset += name_len;
399
 		offset += name_len;
366
 		offset = ( ( offset + 0x03 ) & ~0x03 );
400
 		offset = ( ( offset + 0x03 ) & ~0x03 );

Loading…
Cancel
Save