~pmdj/ubuntu/trusty/qemu/2.9+applesmc+fadtv3

« back to all changes in this revision

Viewing changes to roms/ipxe/src/arch/x86/image/bzimage.c

  • Committer: Phil Dennis-Jordan
  • Date: 2017-07-21 08:03:43 UTC
  • mfrom: (1.1.1)
  • Revision ID: phil@philjordan.eu-20170721080343-2yr2vdj7713czahv
New upstream release 2.9.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public License as
 
6
 * published by the Free Software Foundation; either version 2 of the
 
7
 * License, or any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful, but
 
10
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
17
 * 02110-1301, USA.
 
18
 *
 
19
 * You can also choose to distribute this program under the terms of
 
20
 * the Unmodified Binary Distribution Licence (as given in the file
 
21
 * COPYING.UBDL), provided that you have satisfied its requirements.
 
22
 */
 
23
 
 
24
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
 
25
 
 
26
/**
 
27
 * @file
 
28
 *
 
29
 * Linux bzImage image format
 
30
 *
 
31
 */
 
32
 
 
33
#include <stdint.h>
 
34
#include <stdlib.h>
 
35
#include <string.h>
 
36
#include <errno.h>
 
37
#include <assert.h>
 
38
#include <realmode.h>
 
39
#include <bzimage.h>
 
40
#include <initrd.h>
 
41
#include <ipxe/uaccess.h>
 
42
#include <ipxe/image.h>
 
43
#include <ipxe/segment.h>
 
44
#include <ipxe/init.h>
 
45
#include <ipxe/cpio.h>
 
46
#include <ipxe/features.h>
 
47
 
 
48
FEATURE ( FEATURE_IMAGE, "bzImage", DHCP_EB_FEATURE_BZIMAGE, 1 );
 
49
 
 
50
/**
 
51
 * bzImage context
 
52
 */
 
53
struct bzimage_context {
 
54
        /** Boot protocol version */
 
55
        unsigned int version;
 
56
        /** Real-mode kernel portion load segment address */
 
57
        unsigned int rm_kernel_seg;
 
58
        /** Real-mode kernel portion load address */
 
59
        userptr_t rm_kernel;
 
60
        /** Real-mode kernel portion file size */
 
61
        size_t rm_filesz;
 
62
        /** Real-mode heap top (offset from rm_kernel) */
 
63
        size_t rm_heap;
 
64
        /** Command line (offset from rm_kernel) */
 
65
        size_t rm_cmdline;
 
66
        /** Command line maximum length */
 
67
        size_t cmdline_size;
 
68
        /** Real-mode kernel portion total memory size */
 
69
        size_t rm_memsz;
 
70
        /** Non-real-mode kernel portion load address */
 
71
        userptr_t pm_kernel;
 
72
        /** Non-real-mode kernel portion file and memory size */
 
73
        size_t pm_sz;
 
74
        /** Video mode */
 
75
        unsigned int vid_mode;
 
76
        /** Memory limit */
 
77
        uint64_t mem_limit;
 
78
        /** Initrd address */
 
79
        physaddr_t ramdisk_image;
 
80
        /** Initrd size */
 
81
        physaddr_t ramdisk_size;
 
82
 
 
83
        /** Command line magic block */
 
84
        struct bzimage_cmdline cmdline_magic;
 
85
        /** bzImage header */
 
86
        struct bzimage_header bzhdr;
 
87
};
 
88
 
 
89
/**
 
90
 * Parse bzImage header
 
91
 *
 
92
 * @v image             bzImage file
 
93
 * @v bzimg             bzImage context
 
94
 * @v src               bzImage to parse
 
95
 * @ret rc              Return status code
 
96
 */
 
97
static int bzimage_parse_header ( struct image *image,
 
98
                                  struct bzimage_context *bzimg,
 
99
                                  userptr_t src ) {
 
100
        unsigned int syssize;
 
101
        int is_bzimage;
 
102
 
 
103
        /* Sanity check */
 
104
        if ( image->len < ( BZI_HDR_OFFSET + sizeof ( bzimg->bzhdr ) ) ) {
 
105
                DBGC ( image, "bzImage %p too short for kernel header\n",
 
106
                       image );
 
107
                return -ENOEXEC;
 
108
        }
 
109
 
 
110
        /* Read in header structures */
 
111
        memset ( bzimg, 0, sizeof ( *bzimg ) );
 
112
        copy_from_user ( &bzimg->cmdline_magic, src, BZI_CMDLINE_OFFSET,
 
113
                         sizeof ( bzimg->cmdline_magic ) );
 
114
        copy_from_user ( &bzimg->bzhdr, src, BZI_HDR_OFFSET,
 
115
                         sizeof ( bzimg->bzhdr ) );
 
116
 
 
117
        /* Calculate size of real-mode portion */
 
118
        bzimg->rm_filesz = ( ( ( bzimg->bzhdr.setup_sects ?
 
119
                                 bzimg->bzhdr.setup_sects : 4 ) + 1 ) << 9 );
 
120
        if ( bzimg->rm_filesz > image->len ) {
 
121
                DBGC ( image, "bzImage %p too short for %zd byte of setup\n",
 
122
                       image, bzimg->rm_filesz );
 
123
                return -ENOEXEC;
 
124
        }
 
125
        bzimg->rm_memsz = BZI_ASSUMED_RM_SIZE;
 
126
 
 
127
        /* Calculate size of protected-mode portion */
 
128
        bzimg->pm_sz = ( image->len - bzimg->rm_filesz );
 
129
        syssize = ( ( bzimg->pm_sz + 15 ) / 16 );
 
130
 
 
131
        /* Check for signatures and determine version */
 
132
        if ( bzimg->bzhdr.boot_flag != BZI_BOOT_FLAG ) {
 
133
                DBGC ( image, "bzImage %p missing 55AA signature\n", image );
 
134
                return -ENOEXEC;
 
135
        }
 
136
        if ( bzimg->bzhdr.header == BZI_SIGNATURE ) {
 
137
                /* 2.00+ */
 
138
                bzimg->version = bzimg->bzhdr.version;
 
139
        } else {
 
140
                /* Pre-2.00.  Check that the syssize field is correct,
 
141
                 * as a guard against accepting arbitrary binary data,
 
142
                 * since the 55AA check is pretty lax.  Note that the
 
143
                 * syssize field is unreliable for protocols between
 
144
                 * 2.00 and 2.03 inclusive, so we should not always
 
145
                 * check this field.
 
146
                 */
 
147
                bzimg->version = 0x0100;
 
148
                if ( bzimg->bzhdr.syssize != syssize ) {
 
149
                        DBGC ( image, "bzImage %p bad syssize %x (expected "
 
150
                               "%x)\n", image, bzimg->bzhdr.syssize, syssize );
 
151
                        return -ENOEXEC;
 
152
                }
 
153
        }
 
154
 
 
155
        /* Determine image type */
 
156
        is_bzimage = ( ( bzimg->version >= 0x0200 ) ?
 
157
                       ( bzimg->bzhdr.loadflags & BZI_LOAD_HIGH ) : 0 );
 
158
 
 
159
        /* Calculate load address of real-mode portion */
 
160
        bzimg->rm_kernel_seg = ( is_bzimage ? 0x1000 : 0x9000 );
 
161
        bzimg->rm_kernel = real_to_user ( bzimg->rm_kernel_seg, 0 );
 
162
 
 
163
        /* Allow space for the stack and heap */
 
164
        bzimg->rm_memsz += BZI_STACK_SIZE;
 
165
        bzimg->rm_heap = bzimg->rm_memsz;
 
166
 
 
167
        /* Allow space for the command line */
 
168
        bzimg->rm_cmdline = bzimg->rm_memsz;
 
169
        bzimg->rm_memsz += BZI_CMDLINE_SIZE;
 
170
 
 
171
        /* Calculate load address of protected-mode portion */
 
172
        bzimg->pm_kernel = phys_to_user ( is_bzimage ? BZI_LOAD_HIGH_ADDR
 
173
                                        : BZI_LOAD_LOW_ADDR );
 
174
 
 
175
        /* Extract video mode */
 
176
        bzimg->vid_mode = bzimg->bzhdr.vid_mode;
 
177
 
 
178
        /* Extract memory limit */
 
179
        bzimg->mem_limit = ( ( bzimg->version >= 0x0203 ) ?
 
180
                             bzimg->bzhdr.initrd_addr_max : BZI_INITRD_MAX );
 
181
 
 
182
        /* Extract command line size */
 
183
        bzimg->cmdline_size = ( ( bzimg->version >= 0x0206 ) ?
 
184
                                bzimg->bzhdr.cmdline_size : BZI_CMDLINE_SIZE );
 
185
 
 
186
        DBGC ( image, "bzImage %p version %04x RM %#lx+%#zx PM %#lx+%#zx "
 
187
               "cmdlen %zd\n", image, bzimg->version,
 
188
               user_to_phys ( bzimg->rm_kernel, 0 ), bzimg->rm_filesz,
 
189
               user_to_phys ( bzimg->pm_kernel, 0 ), bzimg->pm_sz,
 
190
               bzimg->cmdline_size );
 
191
 
 
192
        return 0;
 
193
}
 
194
 
 
195
/**
 
196
 * Update bzImage header in loaded kernel
 
197
 *
 
198
 * @v image             bzImage file
 
199
 * @v bzimg             bzImage context
 
200
 * @v dst               bzImage to update
 
201
 */
 
202
static void bzimage_update_header ( struct image *image,
 
203
                                    struct bzimage_context *bzimg,
 
204
                                    userptr_t dst ) {
 
205
 
 
206
        /* Set loader type */
 
207
        if ( bzimg->version >= 0x0200 )
 
208
                bzimg->bzhdr.type_of_loader = BZI_LOADER_TYPE_IPXE;
 
209
 
 
210
        /* Set heap end pointer */
 
211
        if ( bzimg->version >= 0x0201 ) {
 
212
                bzimg->bzhdr.heap_end_ptr = ( bzimg->rm_heap - 0x200 );
 
213
                bzimg->bzhdr.loadflags |= BZI_CAN_USE_HEAP;
 
214
        }
 
215
 
 
216
        /* Set command line */
 
217
        if ( bzimg->version >= 0x0202 ) {
 
218
                bzimg->bzhdr.cmd_line_ptr = user_to_phys ( bzimg->rm_kernel,
 
219
                                                           bzimg->rm_cmdline );
 
220
        } else {
 
221
                bzimg->cmdline_magic.magic = BZI_CMDLINE_MAGIC;
 
222
                bzimg->cmdline_magic.offset = bzimg->rm_cmdline;
 
223
                if ( bzimg->version >= 0x0200 )
 
224
                        bzimg->bzhdr.setup_move_size = bzimg->rm_memsz;
 
225
        }
 
226
 
 
227
        /* Set video mode */
 
228
        bzimg->bzhdr.vid_mode = bzimg->vid_mode;
 
229
 
 
230
        /* Set initrd address */
 
231
        if ( bzimg->version >= 0x0200 ) {
 
232
                bzimg->bzhdr.ramdisk_image = bzimg->ramdisk_image;
 
233
                bzimg->bzhdr.ramdisk_size = bzimg->ramdisk_size;
 
234
        }
 
235
 
 
236
        /* Write out header structures */
 
237
        copy_to_user ( dst, BZI_CMDLINE_OFFSET, &bzimg->cmdline_magic,
 
238
                       sizeof ( bzimg->cmdline_magic ) );
 
239
        copy_to_user ( dst, BZI_HDR_OFFSET, &bzimg->bzhdr,
 
240
                       sizeof ( bzimg->bzhdr ) );
 
241
 
 
242
        DBGC ( image, "bzImage %p vidmode %d\n", image, bzimg->vid_mode );
 
243
}
 
244
 
 
245
/**
 
246
 * Parse kernel command line for bootloader parameters
 
247
 *
 
248
 * @v image             bzImage file
 
249
 * @v bzimg             bzImage context
 
250
 * @v cmdline           Kernel command line
 
251
 * @ret rc              Return status code
 
252
 */
 
253
static int bzimage_parse_cmdline ( struct image *image,
 
254
                                   struct bzimage_context *bzimg,
 
255
                                   const char *cmdline ) {
 
256
        char *vga;
 
257
        char *mem;
 
258
 
 
259
        /* Look for "vga=" */
 
260
        if ( ( vga = strstr ( cmdline, "vga=" ) ) ) {
 
261
                vga += 4;
 
262
                if ( strcmp ( vga, "normal" ) == 0 ) {
 
263
                        bzimg->vid_mode = BZI_VID_MODE_NORMAL;
 
264
                } else if ( strcmp ( vga, "ext" ) == 0 ) {
 
265
                        bzimg->vid_mode = BZI_VID_MODE_EXT;
 
266
                } else if ( strcmp ( vga, "ask" ) == 0 ) {
 
267
                        bzimg->vid_mode = BZI_VID_MODE_ASK;
 
268
                } else {
 
269
                        bzimg->vid_mode = strtoul ( vga, &vga, 0 );
 
270
                        if ( *vga && ( *vga != ' ' ) ) {
 
271
                                DBGC ( image, "bzImage %p strange \"vga=\""
 
272
                                       "terminator '%c'\n", image, *vga );
 
273
                        }
 
274
                }
 
275
        }
 
276
 
 
277
        /* Look for "mem=" */
 
278
        if ( ( mem = strstr ( cmdline, "mem=" ) ) ) {
 
279
                mem += 4;
 
280
                bzimg->mem_limit = strtoul ( mem, &mem, 0 );
 
281
                switch ( *mem ) {
 
282
                case 'G':
 
283
                case 'g':
 
284
                        bzimg->mem_limit <<= 10;
 
285
                case 'M':
 
286
                case 'm':
 
287
                        bzimg->mem_limit <<= 10;
 
288
                case 'K':
 
289
                case 'k':
 
290
                        bzimg->mem_limit <<= 10;
 
291
                        break;
 
292
                case '\0':
 
293
                case ' ':
 
294
                        break;
 
295
                default:
 
296
                        DBGC ( image, "bzImage %p strange \"mem=\" "
 
297
                               "terminator '%c'\n", image, *mem );
 
298
                        break;
 
299
                }
 
300
                bzimg->mem_limit -= 1;
 
301
        }
 
302
 
 
303
        return 0;
 
304
}
 
305
 
 
306
/**
 
307
 * Set command line
 
308
 *
 
309
 * @v image             bzImage image
 
310
 * @v bzimg             bzImage context
 
311
 * @v cmdline           Kernel command line
 
312
 */
 
313
static void bzimage_set_cmdline ( struct image *image,
 
314
                                  struct bzimage_context *bzimg,
 
315
                                  const char *cmdline ) {
 
316
        size_t cmdline_len;
 
317
 
 
318
        /* Copy command line down to real-mode portion */
 
319
        cmdline_len = ( strlen ( cmdline ) + 1 );
 
320
        if ( cmdline_len > bzimg->cmdline_size )
 
321
                cmdline_len = bzimg->cmdline_size;
 
322
        copy_to_user ( bzimg->rm_kernel, bzimg->rm_cmdline,
 
323
                       cmdline, cmdline_len );
 
324
        DBGC ( image, "bzImage %p command line \"%s\"\n", image, cmdline );
 
325
}
 
326
 
 
327
/**
 
328
 * Parse standalone image command line for cpio parameters
 
329
 *
 
330
 * @v image             bzImage file
 
331
 * @v cpio              CPIO header
 
332
 * @v cmdline           Command line
 
333
 */
 
334
static void bzimage_parse_cpio_cmdline ( struct image *image,
 
335
                                         struct cpio_header *cpio,
 
336
                                         const char *cmdline ) {
 
337
        char *arg;
 
338
        char *end;
 
339
        unsigned int mode;
 
340
 
 
341
        /* Look for "mode=" */
 
342
        if ( ( arg = strstr ( cmdline, "mode=" ) ) ) {
 
343
                arg += 5;
 
344
                mode = strtoul ( arg, &end, 8 /* Octal for file mode */ );
 
345
                if ( *end && ( *end != ' ' ) ) {
 
346
                        DBGC ( image, "bzImage %p strange \"mode=\""
 
347
                               "terminator '%c'\n", image, *end );
 
348
                }
 
349
                cpio_set_field ( cpio->c_mode, ( 0100000 | mode ) );
 
350
        }
 
351
}
 
352
 
 
353
/**
 
354
 * Align initrd length
 
355
 *
 
356
 * @v len               Length
 
357
 * @ret len             Length rounded up to INITRD_ALIGN
 
358
 */
 
359
static inline size_t bzimage_align ( size_t len ) {
 
360
 
 
361
        return ( ( len + INITRD_ALIGN - 1 ) & ~( INITRD_ALIGN - 1 ) );
 
362
}
 
363
 
 
364
/**
 
365
 * Load initrd
 
366
 *
 
367
 * @v image             bzImage image
 
368
 * @v initrd            initrd image
 
369
 * @v address           Address at which to load, or UNULL
 
370
 * @ret len             Length of loaded image, excluding zero-padding
 
371
 */
 
372
static size_t bzimage_load_initrd ( struct image *image,
 
373
                                    struct image *initrd,
 
374
                                    userptr_t address ) {
 
375
        char *filename = initrd->cmdline;
 
376
        char *cmdline;
 
377
        struct cpio_header cpio;
 
378
        size_t offset;
 
379
        size_t name_len;
 
380
        size_t pad_len;
 
381
 
 
382
        /* Do not include kernel image itself as an initrd */
 
383
        if ( initrd == image )
 
384
                return 0;
 
385
 
 
386
        /* Create cpio header for non-prebuilt images */
 
387
        if ( filename && filename[0] ) {
 
388
                cmdline = strchr ( filename, ' ' );
 
389
                name_len = ( ( cmdline ? ( ( size_t ) ( cmdline - filename ) )
 
390
                               : strlen ( filename ) ) + 1 /* NUL */ );
 
391
                memset ( &cpio, '0', sizeof ( cpio ) );
 
392
                memcpy ( cpio.c_magic, CPIO_MAGIC, sizeof ( cpio.c_magic ) );
 
393
                cpio_set_field ( cpio.c_mode, 0100644 );
 
394
                cpio_set_field ( cpio.c_nlink, 1 );
 
395
                cpio_set_field ( cpio.c_filesize, initrd->len );
 
396
                cpio_set_field ( cpio.c_namesize, name_len );
 
397
                if ( cmdline ) {
 
398
                        bzimage_parse_cpio_cmdline ( image, &cpio,
 
399
                                                     ( cmdline + 1 /* ' ' */ ));
 
400
                }
 
401
                offset = ( ( sizeof ( cpio ) + name_len + 0x03 ) & ~0x03 );
 
402
        } else {
 
403
                offset = 0;
 
404
                name_len = 0;
 
405
        }
 
406
 
 
407
        /* Copy in initrd image body (and cpio header if applicable) */
 
408
        if ( address ) {
 
409
                memmove_user ( address, offset, initrd->data, 0, initrd->len );
 
410
                if ( offset ) {
 
411
                        memset_user ( address, 0, 0, offset );
 
412
                        copy_to_user ( address, 0, &cpio, sizeof ( cpio ) );
 
413
                        copy_to_user ( address, sizeof ( cpio ), filename,
 
414
                                       ( name_len - 1 /* NUL (or space) */ ) );
 
415
                }
 
416
                DBGC ( image, "bzImage %p initrd %p [%#08lx,%#08lx,%#08lx)"
 
417
                       "%s%s\n", image, initrd, user_to_phys ( address, 0 ),
 
418
                       user_to_phys ( address, offset ),
 
419
                       user_to_phys ( address, ( offset + initrd->len ) ),
 
420
                       ( filename ? " " : "" ), ( filename ? filename : "" ) );
 
421
                DBGC2_MD5A ( image, user_to_phys ( address, offset ),
 
422
                             user_to_virt ( address, offset ), initrd->len );
 
423
        }
 
424
        offset += initrd->len;
 
425
 
 
426
        /* Zero-pad to next INITRD_ALIGN boundary */
 
427
        pad_len = ( ( -offset ) & ( INITRD_ALIGN - 1 ) );
 
428
        if ( address )
 
429
                memset_user ( address, offset, 0, pad_len );
 
430
 
 
431
        return offset;
 
432
}
 
433
 
 
434
/**
 
435
 * Check that initrds can be loaded
 
436
 *
 
437
 * @v image             bzImage image
 
438
 * @v bzimg             bzImage context
 
439
 * @ret rc              Return status code
 
440
 */
 
441
static int bzimage_check_initrds ( struct image *image,
 
442
                                   struct bzimage_context *bzimg ) {
 
443
        struct image *initrd;
 
444
        userptr_t bottom;
 
445
        size_t len = 0;
 
446
        int rc;
 
447
 
 
448
        /* Calculate total loaded length of initrds */
 
449
        for_each_image ( initrd ) {
 
450
 
 
451
                /* Skip kernel */
 
452
                if ( initrd == image )
 
453
                        continue;
 
454
 
 
455
                /* Calculate length */
 
456
                len += bzimage_load_initrd ( image, initrd, UNULL );
 
457
                len = bzimage_align ( len );
 
458
 
 
459
                DBGC ( image, "bzImage %p initrd %p from [%#08lx,%#08lx)%s%s\n",
 
460
                       image, initrd, user_to_phys ( initrd->data, 0 ),
 
461
                       user_to_phys ( initrd->data, initrd->len ),
 
462
                       ( initrd->cmdline ? " " : "" ),
 
463
                       ( initrd->cmdline ? initrd->cmdline : "" ) );
 
464
                DBGC2_MD5A ( image, user_to_phys ( initrd->data, 0 ),
 
465
                             user_to_virt ( initrd->data, 0 ), initrd->len );
 
466
        }
 
467
 
 
468
        /* Calculate lowest usable address */
 
469
        bottom = userptr_add ( bzimg->pm_kernel, bzimg->pm_sz );
 
470
 
 
471
        /* Check that total length fits within space available for
 
472
         * reshuffling.  This is a conservative check, since CPIO
 
473
         * headers are not present during reshuffling, but this
 
474
         * doesn't hurt and keeps the code simple.
 
475
         */
 
476
        if ( ( rc = initrd_reshuffle_check ( len, bottom ) ) != 0 ) {
 
477
                DBGC ( image, "bzImage %p failed reshuffle check: %s\n",
 
478
                       image, strerror ( rc ) );
 
479
                return rc;
 
480
        }
 
481
 
 
482
        /* Check that total length fits within kernel's memory limit */
 
483
        if ( user_to_phys ( bottom, len ) > bzimg->mem_limit ) {
 
484
                DBGC ( image, "bzImage %p not enough space for initrds\n",
 
485
                       image );
 
486
                return -ENOBUFS;
 
487
        }
 
488
 
 
489
        return 0;
 
490
}
 
491
 
 
492
/**
 
493
 * Load initrds, if any
 
494
 *
 
495
 * @v image             bzImage image
 
496
 * @v bzimg             bzImage context
 
497
 */
 
498
static void bzimage_load_initrds ( struct image *image,
 
499
                                   struct bzimage_context *bzimg ) {
 
500
        struct image *initrd;
 
501
        struct image *highest = NULL;
 
502
        struct image *other;
 
503
        userptr_t top;
 
504
        userptr_t dest;
 
505
        size_t offset;
 
506
        size_t len;
 
507
 
 
508
        /* Reshuffle initrds into desired order */
 
509
        initrd_reshuffle ( userptr_add ( bzimg->pm_kernel, bzimg->pm_sz ) );
 
510
 
 
511
        /* Find highest initrd */
 
512
        for_each_image ( initrd ) {
 
513
                if ( ( highest == NULL ) ||
 
514
                     ( userptr_sub ( initrd->data, highest->data ) > 0 ) ) {
 
515
                        highest = initrd;
 
516
                }
 
517
        }
 
518
 
 
519
        /* Do nothing if there are no initrds */
 
520
        if ( ! highest )
 
521
                return;
 
522
 
 
523
        /* Find highest usable address */
 
524
        top = userptr_add ( highest->data, bzimage_align ( highest->len ) );
 
525
        if ( user_to_phys ( top, -1 ) > bzimg->mem_limit ) {
 
526
                top = phys_to_user ( ( bzimg->mem_limit + 1 ) &
 
527
                                     ~( INITRD_ALIGN - 1 ) );
 
528
        }
 
529
        DBGC ( image, "bzImage %p loading initrds from %#08lx downwards\n",
 
530
               image, user_to_phys ( top, -1 ) );
 
531
 
 
532
        /* Load initrds in order */
 
533
        for_each_image ( initrd ) {
 
534
 
 
535
                /* Calculate cumulative length of following
 
536
                 * initrds (including padding).
 
537
                 */
 
538
                offset = 0;
 
539
                for_each_image ( other ) {
 
540
                        if ( other == initrd )
 
541
                                offset = 0;
 
542
                        offset += bzimage_load_initrd ( image, other, UNULL );
 
543
                        offset = bzimage_align ( offset );
 
544
                }
 
545
 
 
546
                /* Load initrd at this address */
 
547
                dest = userptr_add ( top, -offset );
 
548
                len = bzimage_load_initrd ( image, initrd, dest );
 
549
 
 
550
                /* Record initrd location */
 
551
                if ( ! bzimg->ramdisk_image )
 
552
                        bzimg->ramdisk_image = user_to_phys ( dest, 0 );
 
553
                bzimg->ramdisk_size = ( user_to_phys ( dest, len ) -
 
554
                                        bzimg->ramdisk_image );
 
555
        }
 
556
        DBGC ( image, "bzImage %p initrds at [%#08lx,%#08lx)\n",
 
557
               image, bzimg->ramdisk_image,
 
558
               ( bzimg->ramdisk_image + bzimg->ramdisk_size ) );
 
559
}
 
560
 
 
561
/**
 
562
 * Execute bzImage image
 
563
 *
 
564
 * @v image             bzImage image
 
565
 * @ret rc              Return status code
 
566
 */
 
567
static int bzimage_exec ( struct image *image ) {
 
568
        struct bzimage_context bzimg;
 
569
        const char *cmdline = ( image->cmdline ? image->cmdline : "" );
 
570
        int rc;
 
571
 
 
572
        /* Read and parse header from image */
 
573
        if ( ( rc = bzimage_parse_header ( image, &bzimg,
 
574
                                           image->data ) ) != 0 )
 
575
                return rc;
 
576
 
 
577
        /* Prepare segments */
 
578
        if ( ( rc = prep_segment ( bzimg.rm_kernel, bzimg.rm_filesz,
 
579
                                   bzimg.rm_memsz ) ) != 0 ) {
 
580
                DBGC ( image, "bzImage %p could not prepare RM segment: %s\n",
 
581
                       image, strerror ( rc ) );
 
582
                return rc;
 
583
        }
 
584
        if ( ( rc = prep_segment ( bzimg.pm_kernel, bzimg.pm_sz,
 
585
                                   bzimg.pm_sz ) ) != 0 ) {
 
586
                DBGC ( image, "bzImage %p could not prepare PM segment: %s\n",
 
587
                       image, strerror ( rc ) );
 
588
                return rc;
 
589
        }
 
590
 
 
591
        /* Parse command line for bootloader parameters */
 
592
        if ( ( rc = bzimage_parse_cmdline ( image, &bzimg, cmdline ) ) != 0)
 
593
                return rc;
 
594
 
 
595
        /* Check that initrds can be loaded */
 
596
        if ( ( rc = bzimage_check_initrds ( image, &bzimg ) ) != 0 )
 
597
                return rc;
 
598
 
 
599
        /* Remove kernel from image list (without invalidating image pointer) */
 
600
        unregister_image ( image_get ( image ) );
 
601
 
 
602
        /* Load segments */
 
603
        memcpy_user ( bzimg.rm_kernel, 0, image->data,
 
604
                      0, bzimg.rm_filesz );
 
605
        memcpy_user ( bzimg.pm_kernel, 0, image->data,
 
606
                      bzimg.rm_filesz, bzimg.pm_sz );
 
607
 
 
608
        /* Store command line */
 
609
        bzimage_set_cmdline ( image, &bzimg, cmdline );
 
610
 
 
611
        /* Prepare for exiting.  Must do this before loading initrds,
 
612
         * since loading the initrds will corrupt the external heap.
 
613
         */
 
614
        shutdown_boot();
 
615
 
 
616
        /* Load any initrds */
 
617
        bzimage_load_initrds ( image, &bzimg );
 
618
 
 
619
        /* Update kernel header */
 
620
        bzimage_update_header ( image, &bzimg, bzimg.rm_kernel );
 
621
 
 
622
        DBGC ( image, "bzImage %p jumping to RM kernel at %04x:0000 "
 
623
               "(stack %04x:%04zx)\n", image, ( bzimg.rm_kernel_seg + 0x20 ),
 
624
               bzimg.rm_kernel_seg, bzimg.rm_heap );
 
625
 
 
626
        /* Jump to the kernel */
 
627
        __asm__ __volatile__ ( REAL_CODE ( "movw %w0, %%ds\n\t"
 
628
                                           "movw %w0, %%es\n\t"
 
629
                                           "movw %w0, %%fs\n\t"
 
630
                                           "movw %w0, %%gs\n\t"
 
631
                                           "movw %w0, %%ss\n\t"
 
632
                                           "movw %w1, %%sp\n\t"
 
633
                                           "pushw %w2\n\t"
 
634
                                           "pushw $0\n\t"
 
635
                                           "lret\n\t" )
 
636
                               : : "R" ( bzimg.rm_kernel_seg ),
 
637
                                   "R" ( bzimg.rm_heap ),
 
638
                                   "R" ( bzimg.rm_kernel_seg + 0x20 ) );
 
639
 
 
640
        /* There is no way for the image to return, since we provide
 
641
         * no return address.
 
642
         */
 
643
        assert ( 0 );
 
644
 
 
645
        return -ECANCELED; /* -EIMPOSSIBLE */
 
646
}
 
647
 
 
648
/**
 
649
 * Probe bzImage image
 
650
 *
 
651
 * @v image             bzImage file
 
652
 * @ret rc              Return status code
 
653
 */
 
654
int bzimage_probe ( struct image *image ) {
 
655
        struct bzimage_context bzimg;
 
656
        int rc;
 
657
 
 
658
        /* Read and parse header from image */
 
659
        if ( ( rc = bzimage_parse_header ( image, &bzimg,
 
660
                                           image->data ) ) != 0 )
 
661
                return rc;
 
662
 
 
663
        return 0;
 
664
}
 
665
 
 
666
/** Linux bzImage image type */
 
667
struct image_type bzimage_image_type __image_type ( PROBE_NORMAL ) = {
 
668
        .name = "bzImage",
 
669
        .probe = bzimage_probe,
 
670
        .exec = bzimage_exec,
 
671
};