~ubuntu-branches/ubuntu/raring/ipxe/raring

« back to all changes in this revision

Viewing changes to src/arch/i386/image/bzimage.c

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2012-11-14 15:47:31 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20121114154731-jhuy5d1h2jw75qe9
Tags: 1.0.0+git-4.d6b0b76-0ubuntu1
* New upstream snapshot:
  - d/p/iscsi*.patch: Dropped - included in snapshot.
  - Refreshed all other patches.
* d/p/enable-https.patch: Enable HTTPS support (LP: #1025239).

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
 *
14
14
 * You should have received a copy of the GNU General Public License
15
15
 * along with this program; if not, write to the Free Software
16
 
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
17
 * 02110-1301, USA.
17
18
 */
18
19
 
19
20
FILE_LICENCE ( GPL2_OR_LATER );
32
33
#include <assert.h>
33
34
#include <realmode.h>
34
35
#include <bzimage.h>
 
36
#include <initrd.h>
35
37
#include <ipxe/uaccess.h>
36
38
#include <ipxe/image.h>
37
39
#include <ipxe/segment.h>
214
216
        } else {
215
217
                bzimg->cmdline_magic.magic = BZI_CMDLINE_MAGIC;
216
218
                bzimg->cmdline_magic.offset = bzimg->rm_cmdline;
217
 
                bzimg->bzhdr.setup_move_size = bzimg->rm_memsz;
 
219
                if ( bzimg->version >= 0x0200 )
 
220
                        bzimg->bzhdr.setup_move_size = bzimg->rm_memsz;
218
221
        }
219
222
 
220
223
        /* Set video mode */
302
305
 * @v image             bzImage image
303
306
 * @v bzimg             bzImage context
304
307
 * @v cmdline           Kernel command line
305
 
 * @ret rc              Return status code
306
308
 */
307
 
static int bzimage_set_cmdline ( struct image *image,
308
 
                                 struct bzimage_context *bzimg,
309
 
                                 const char *cmdline ) {
 
309
static void bzimage_set_cmdline ( struct image *image,
 
310
                                  struct bzimage_context *bzimg,
 
311
                                  const char *cmdline ) {
310
312
        size_t cmdline_len;
311
313
 
312
314
        /* Copy command line down to real-mode portion */
316
318
        copy_to_user ( bzimg->rm_kernel, bzimg->rm_cmdline,
317
319
                       cmdline, cmdline_len );
318
320
        DBGC ( image, "bzImage %p command line \"%s\"\n", image, cmdline );
319
 
 
320
 
        return 0;
 
321
}
 
322
 
 
323
/**
 
324
 * Parse standalone image command line for cpio parameters
 
325
 *
 
326
 * @v image             bzImage file
 
327
 * @v cpio              CPIO header
 
328
 * @v cmdline           Command line
 
329
 */
 
330
static void bzimage_parse_cpio_cmdline ( struct image *image,
 
331
                                         struct cpio_header *cpio,
 
332
                                         const char *cmdline ) {
 
333
        char *arg;
 
334
        char *end;
 
335
        unsigned int mode;
 
336
 
 
337
        /* Look for "mode=" */
 
338
        if ( ( arg = strstr ( cmdline, "mode=" ) ) ) {
 
339
                arg += 5;
 
340
                mode = strtoul ( arg, &end, 8 /* Octal for file mode */ );
 
341
                if ( *end && ( *end != ' ' ) ) {
 
342
                        DBGC ( image, "bzImage %p strange \"mode=\""
 
343
                               "terminator '%c'\n", image, *end );
 
344
                }
 
345
                cpio_set_field ( cpio->c_mode, ( 0100000 | mode ) );
 
346
        }
321
347
}
322
348
 
323
349
/**
326
352
 * @v image             bzImage image
327
353
 * @v initrd            initrd image
328
354
 * @v address           Address at which to load, or UNULL
329
 
 * @ret len             Length of loaded image, rounded up to 4 bytes
 
355
 * @ret len             Length of loaded image, rounded up to INITRD_ALIGN
330
356
 */
331
357
static size_t bzimage_load_initrd ( struct image *image,
332
358
                                    struct image *initrd,
333
359
                                    userptr_t address ) {
334
360
        char *filename = initrd->cmdline;
 
361
        char *cmdline;
335
362
        struct cpio_header cpio;
336
363
        size_t offset = 0;
 
364
        size_t name_len;
 
365
        size_t pad_len;
337
366
 
338
367
        /* Do not include kernel image itself as an initrd */
339
368
        if ( initrd == image )
341
370
 
342
371
        /* Create cpio header before non-prebuilt images */
343
372
        if ( filename && filename[0] ) {
344
 
                size_t name_len = ( strlen ( filename ) + 1 );
345
 
 
346
 
                DBGC ( image, "bzImage %p inserting initrd %p as %s\n",
347
 
                       image, initrd, filename );
 
373
                cmdline = strchr ( filename, ' ' );
 
374
                name_len = ( ( cmdline ? ( ( size_t ) ( cmdline - filename ) )
 
375
                               : strlen ( filename ) ) + 1 /* NUL */ );
348
376
                memset ( &cpio, '0', sizeof ( cpio ) );
349
377
                memcpy ( cpio.c_magic, CPIO_MAGIC, sizeof ( cpio.c_magic ) );
350
378
                cpio_set_field ( cpio.c_mode, 0100644 );
351
379
                cpio_set_field ( cpio.c_nlink, 1 );
352
380
                cpio_set_field ( cpio.c_filesize, initrd->len );
353
381
                cpio_set_field ( cpio.c_namesize, name_len );
 
382
                if ( cmdline ) {
 
383
                        bzimage_parse_cpio_cmdline ( image, &cpio,
 
384
                                                     ( cmdline + 1 /* ' ' */ ));
 
385
                }
354
386
                if ( address ) {
355
387
                        copy_to_user ( address, offset, &cpio,
356
388
                                       sizeof ( cpio ) );
357
389
                }
358
390
                offset += sizeof ( cpio );
359
391
                if ( address ) {
 
392
                        memset_user ( address, offset, 0, name_len );
360
393
                        copy_to_user ( address, offset, filename,
361
 
                                       name_len );
 
394
                                       ( name_len - 1 /* NUL (or space) */ ) );
362
395
                }
363
396
                offset += name_len;
364
397
                offset = ( ( offset + 0x03 ) & ~0x03 );
366
399
 
367
400
        /* Copy in initrd image body */
368
401
        if ( address )
369
 
                memcpy_user ( address, offset, initrd->data, 0, initrd->len );
 
402
                memmove_user ( address, offset, initrd->data, 0, initrd->len );
 
403
        if ( address ) {
 
404
                DBGC ( image, "bzImage %p initrd %p [%#08lx,%#08lx,%#08lx)"
 
405
                       "%s%s\n", image, initrd, user_to_phys ( address, 0 ),
 
406
                       user_to_phys ( address, offset ),
 
407
                       user_to_phys ( address, ( offset + initrd->len ) ),
 
408
                       ( filename ? " " : "" ), ( filename ? filename : "" ) );
 
409
                DBGC2_MD5A ( image, user_to_phys ( address, offset ),
 
410
                             user_to_virt ( address, offset ), initrd->len );
 
411
        }
370
412
        offset += initrd->len;
371
 
        if ( address ) {
372
 
                DBGC ( image, "bzImage %p has initrd %p at [%lx,%lx)\n",
373
 
                       image, initrd, user_to_phys ( address, 0 ),
374
 
                       user_to_phys ( address, offset ) );
375
 
        }
376
 
 
377
 
        /* Round up to 4-byte boundary */
378
 
        offset = ( ( offset + 0x03 ) & ~0x03 );
 
413
 
 
414
        /* Round up to multiple of INITRD_ALIGN and zero-pad */
 
415
        pad_len = ( ( -offset ) & ( INITRD_ALIGN - 1 ) );
 
416
        if ( address )
 
417
                memset_user ( address, offset, 0, pad_len );
 
418
        offset += pad_len;
 
419
 
379
420
        return offset;
380
421
}
381
422
 
382
423
/**
 
424
 * Check that initrds can be loaded
 
425
 *
 
426
 * @v image             bzImage image
 
427
 * @v bzimg             bzImage context
 
428
 * @ret rc              Return status code
 
429
 */
 
430
static int bzimage_check_initrds ( struct image *image,
 
431
                                   struct bzimage_context *bzimg ) {
 
432
        struct image *initrd;
 
433
        userptr_t bottom;
 
434
        size_t len = 0;
 
435
        int rc;
 
436
 
 
437
        /* Calculate total loaded length of initrds */
 
438
        for_each_image ( initrd ) {
 
439
 
 
440
                /* Skip kernel */
 
441
                if ( initrd == image )
 
442
                        continue;
 
443
 
 
444
                /* Calculate length */
 
445
                len += bzimage_load_initrd ( image, initrd, UNULL );
 
446
 
 
447
                DBGC ( image, "bzImage %p initrd %p from [%#08lx,%#08lx)%s%s\n",
 
448
                       image, initrd, user_to_phys ( initrd->data, 0 ),
 
449
                       user_to_phys ( initrd->data, initrd->len ),
 
450
                       ( initrd->cmdline ? " " : "" ),
 
451
                       ( initrd->cmdline ? initrd->cmdline : "" ) );
 
452
                DBGC2_MD5A ( image, user_to_phys ( initrd->data, 0 ),
 
453
                             user_to_virt ( initrd->data, 0 ), initrd->len );
 
454
        }
 
455
 
 
456
        /* Calculate lowest usable address */
 
457
        bottom = userptr_add ( bzimg->pm_kernel, bzimg->pm_sz );
 
458
 
 
459
        /* Check that total length fits within space available for
 
460
         * reshuffling.  This is a conservative check, since CPIO
 
461
         * headers are not present during reshuffling, but this
 
462
         * doesn't hurt and keeps the code simple.
 
463
         */
 
464
        if ( ( rc = initrd_reshuffle_check ( len, bottom ) ) != 0 ) {
 
465
                DBGC ( image, "bzImage %p failed reshuffle check: %s\n",
 
466
                       image, strerror ( rc ) );
 
467
                return rc;
 
468
        }
 
469
 
 
470
        /* Check that total length fits within kernel's memory limit */
 
471
        if ( user_to_phys ( bottom, len ) > bzimg->mem_limit ) {
 
472
                DBGC ( image, "bzImage %p not enough space for initrds\n",
 
473
                       image );
 
474
                return -ENOBUFS;
 
475
        }
 
476
 
 
477
        return 0;
 
478
}
 
479
 
 
480
/**
383
481
 * Load initrds, if any
384
482
 *
385
483
 * @v image             bzImage image
386
484
 * @v bzimg             bzImage context
387
 
 * @ret rc              Return status code
388
485
 */
389
 
static int bzimage_load_initrds ( struct image *image,
390
 
                                  struct bzimage_context *bzimg ) {
 
486
static void bzimage_load_initrds ( struct image *image,
 
487
                                   struct bzimage_context *bzimg ) {
391
488
        struct image *initrd;
392
 
        size_t total_len = 0;
393
 
        physaddr_t address;
394
 
        int rc;
395
 
 
396
 
        /* Add up length of all initrd images */
397
 
        for_each_image ( initrd )
398
 
                total_len += bzimage_load_initrd ( image, initrd, UNULL );
399
 
 
400
 
        /* Give up if no initrd images found */
401
 
        if ( ! total_len )
402
 
                return 0;
403
 
 
404
 
        /* Find a suitable start address.  Try 1MB boundaries,
405
 
         * starting from the downloaded kernel image itself and
406
 
         * working downwards until we hit an available region.
407
 
         */
408
 
        for ( address = ( user_to_phys ( image->data, 0 ) & ~0xfffff ) ; ;
409
 
              address -= 0x100000 ) {
410
 
                /* Check that we're not going to overwrite the
411
 
                 * kernel itself.  This check isn't totally
412
 
                 * accurate, but errs on the side of caution.
 
489
        struct image *highest = NULL;
 
490
        struct image *other;
 
491
        userptr_t top;
 
492
        userptr_t dest;
 
493
        size_t len;
 
494
 
 
495
        /* Reshuffle initrds into desired order */
 
496
        initrd_reshuffle ( userptr_add ( bzimg->pm_kernel, bzimg->pm_sz ) );
 
497
 
 
498
        /* Find highest initrd */
 
499
        for_each_image ( initrd ) {
 
500
                if ( ( highest == NULL ) ||
 
501
                     ( userptr_sub ( initrd->data, highest->data ) > 0 ) ) {
 
502
                        highest = initrd;
 
503
                }
 
504
        }
 
505
 
 
506
        /* Do nothing if there are no initrds */
 
507
        if ( ! highest )
 
508
                return;
 
509
 
 
510
        /* Find highest usable address */
 
511
        top = userptr_add ( highest->data,
 
512
                            ( ( highest->len + INITRD_ALIGN - 1 ) &
 
513
                              ~( INITRD_ALIGN - 1 ) ) );
 
514
        if ( user_to_phys ( top, 0 ) > bzimg->mem_limit )
 
515
                top = phys_to_user ( bzimg->mem_limit );
 
516
        DBGC ( image, "bzImage %p loading initrds from %#08lx downwards\n",
 
517
               image, user_to_phys ( top, 0 ) );
 
518
 
 
519
        /* Load initrds in order */
 
520
        for_each_image ( initrd ) {
 
521
 
 
522
                /* Calculate cumulative length of following
 
523
                 * initrds (including padding).
413
524
                 */
414
 
                if ( address <= ( BZI_LOAD_HIGH_ADDR + image->len ) ) {
415
 
                        DBGC ( image, "bzImage %p could not find a location "
416
 
                               "for initrd\n", image );
417
 
                        return -ENOBUFS;
418
 
                }
419
 
                /* Check that we are within the kernel's range */
420
 
                if ( ( address + total_len - 1 ) > bzimg->mem_limit )
421
 
                        continue;
422
 
                /* Prepare and verify segment */
423
 
                if ( ( rc = prep_segment ( phys_to_user ( address ), 0,
424
 
                                           total_len ) ) != 0 )
425
 
                        continue;
426
 
                /* Use this address */
427
 
                break;
428
 
        }
429
 
 
430
 
        /* Record initrd location */
431
 
        bzimg->ramdisk_image = address;
432
 
        bzimg->ramdisk_size = total_len;
433
 
 
434
 
        /* Construct initrd */
435
 
        DBGC ( image, "bzImage %p constructing initrd at [%lx,%lx)\n",
436
 
               image, address, ( address + total_len ) );
437
 
        for_each_image ( initrd ) {
438
 
                address += bzimage_load_initrd ( image, initrd,
439
 
                                                 phys_to_user ( address ) );
440
 
        }
441
 
 
442
 
        return 0;
 
525
                len = 0;
 
526
                for_each_image ( other ) {
 
527
                        if ( other == initrd )
 
528
                                len = 0;
 
529
                        len += bzimage_load_initrd ( image, other, UNULL );
 
530
                }
 
531
 
 
532
                /* Load initrd at this address */
 
533
                dest = userptr_add ( top, -len );
 
534
                bzimage_load_initrd ( image, initrd, dest );
 
535
 
 
536
                /* Record initrd location */
 
537
                if ( ! bzimg->ramdisk_image ) {
 
538
                        bzimg->ramdisk_image = user_to_phys ( dest, 0 );
 
539
                        bzimg->ramdisk_size = len;
 
540
                }
 
541
        }
443
542
}
444
543
 
445
544
/**
472
571
                return rc;
473
572
        }
474
573
 
 
574
        /* Parse command line for bootloader parameters */
 
575
        if ( ( rc = bzimage_parse_cmdline ( image, &bzimg, cmdline ) ) != 0)
 
576
                return rc;
 
577
 
 
578
        /* Check that initrds can be loaded */
 
579
        if ( ( rc = bzimage_check_initrds ( image, &bzimg ) ) != 0 )
 
580
                return rc;
 
581
 
 
582
        /* Remove kernel from image list (without invalidating image pointer) */
 
583
        unregister_image ( image_get ( image ) );
 
584
 
475
585
        /* Load segments */
476
586
        memcpy_user ( bzimg.rm_kernel, 0, image->data,
477
587
                      0, bzimg.rm_filesz );
478
588
        memcpy_user ( bzimg.pm_kernel, 0, image->data,
479
589
                      bzimg.rm_filesz, bzimg.pm_sz );
480
590
 
481
 
        /* Update and write out header */
482
 
        bzimage_update_header ( image, &bzimg, bzimg.rm_kernel );
483
 
 
484
 
        /* Parse command line for bootloader parameters */
485
 
        if ( ( rc = bzimage_parse_cmdline ( image, &bzimg, cmdline ) ) != 0)
486
 
                return rc;
487
 
 
488
591
        /* Store command line */
489
 
        if ( ( rc = bzimage_set_cmdline ( image, &bzimg, cmdline ) ) != 0 )
490
 
                return rc;
 
592
        bzimage_set_cmdline ( image, &bzimg, cmdline );
 
593
 
 
594
        /* Prepare for exiting.  Must do this before loading initrds,
 
595
         * since loading the initrds will corrupt the external heap.
 
596
         */
 
597
        shutdown_boot();
491
598
 
492
599
        /* Load any initrds */
493
 
        if ( ( rc = bzimage_load_initrds ( image, &bzimg ) ) != 0 )
494
 
                return rc;
 
600
        bzimage_load_initrds ( image, &bzimg );
495
601
 
496
602
        /* Update kernel header */
497
603
        bzimage_update_header ( image, &bzimg, bzimg.rm_kernel );
498
604
 
499
 
        /* Prepare for exiting */
500
 
        shutdown_boot();
501
 
 
502
605
        DBGC ( image, "bzImage %p jumping to RM kernel at %04x:0000 "
503
606
               "(stack %04x:%04zx)\n", image, ( bzimg.rm_kernel_seg + 0x20 ),
504
607
               bzimg.rm_kernel_seg, bzimg.rm_heap );