|
@@ -35,6 +35,7 @@
|
35
|
35
|
#include <gpxe/segment.h>
|
36
|
36
|
#include <gpxe/init.h>
|
37
|
37
|
#include <gpxe/initrd.h>
|
|
38
|
+#include <gpxe/cpio.h>
|
38
|
39
|
|
39
|
40
|
struct image_type bzimage_image_type __image_type ( PROBE_NORMAL );
|
40
|
41
|
|
|
@@ -166,6 +167,63 @@ static int bzimage_set_cmdline ( struct image *image,
|
166
|
167
|
return 0;
|
167
|
168
|
}
|
168
|
169
|
|
|
170
|
+/**
|
|
171
|
+ * Load initrd
|
|
172
|
+ *
|
|
173
|
+ * @v image bzImage image
|
|
174
|
+ * @v initrd initrd image
|
|
175
|
+ * @v address Address at which to load, or UNULL
|
|
176
|
+ * @ret len Length of loaded image, rounded up to 4 bytes
|
|
177
|
+ */
|
|
178
|
+static size_t bzimage_load_initrd ( struct image *image,
|
|
179
|
+ struct image *initrd,
|
|
180
|
+ userptr_t address ) {
|
|
181
|
+ char *filename = initrd->cmdline;
|
|
182
|
+ struct cpio_header cpio;
|
|
183
|
+ size_t offset = 0;
|
|
184
|
+
|
|
185
|
+ /* Ignore images which aren't initrds */
|
|
186
|
+ if ( initrd->type != &initrd_image_type )
|
|
187
|
+ return 0;
|
|
188
|
+
|
|
189
|
+ /* Create cpio header before non-prebuilt images */
|
|
190
|
+ if ( filename[0] ) {
|
|
191
|
+ size_t name_len = ( strlen ( filename ) + 1 );
|
|
192
|
+
|
|
193
|
+ DBGC ( image, "bzImage %p inserting initrd %p as %s\n",
|
|
194
|
+ image, initrd, filename );
|
|
195
|
+ memset ( &cpio, '0', sizeof ( cpio ) );
|
|
196
|
+ memcpy ( cpio.c_magic, CPIO_MAGIC, sizeof ( cpio.c_magic ) );
|
|
197
|
+ cpio_set_field ( cpio.c_mode, 0100644 );
|
|
198
|
+ cpio_set_field ( cpio.c_nlink, 1 );
|
|
199
|
+ cpio_set_field ( cpio.c_filesize, initrd->len );
|
|
200
|
+ cpio_set_field ( cpio.c_namesize, name_len );
|
|
201
|
+ if ( address ) {
|
|
202
|
+ copy_to_user ( address, offset, &cpio,
|
|
203
|
+ sizeof ( cpio ) );
|
|
204
|
+ }
|
|
205
|
+ offset += sizeof ( cpio );
|
|
206
|
+ if ( address ) {
|
|
207
|
+ copy_to_user ( address, offset, filename,
|
|
208
|
+ name_len );
|
|
209
|
+ }
|
|
210
|
+ offset += name_len;
|
|
211
|
+ offset = ( ( offset + 0x03 ) & ~0x03 );
|
|
212
|
+ }
|
|
213
|
+
|
|
214
|
+ /* Copy in initrd image body */
|
|
215
|
+ if ( address ) {
|
|
216
|
+ DBGC ( image, "bzImage %p has initrd %p at [%lx,%lx)\n",
|
|
217
|
+ image, initrd, address, ( address + offset ) );
|
|
218
|
+ memcpy_user ( address, offset, initrd->data, 0,
|
|
219
|
+ initrd->len );
|
|
220
|
+ }
|
|
221
|
+ offset += initrd->len;
|
|
222
|
+
|
|
223
|
+ offset = ( ( offset + 0x03 ) & ~0x03 );
|
|
224
|
+ return offset;
|
|
225
|
+}
|
|
226
|
+
|
169
|
227
|
/**
|
170
|
228
|
* Load initrds, if any
|
171
|
229
|
*
|
|
@@ -173,21 +231,16 @@ static int bzimage_set_cmdline ( struct image *image,
|
173
|
231
|
* @v exec_ctx Execution context
|
174
|
232
|
* @ret rc Return status code
|
175
|
233
|
*/
|
176
|
|
-static int bzimage_load_initrd ( struct image *image,
|
177
|
|
- struct bzimage_exec_context *exec_ctx ) {
|
|
234
|
+static int bzimage_load_initrds ( struct image *image,
|
|
235
|
+ struct bzimage_exec_context *exec_ctx ) {
|
178
|
236
|
struct image *initrd;
|
179
|
|
- size_t initrd_len;
|
180
|
237
|
size_t total_len = 0;
|
181
|
|
- size_t offset = 0;
|
182
|
|
- physaddr_t start;
|
|
238
|
+ physaddr_t address;
|
183
|
239
|
int rc;
|
184
|
240
|
|
185
|
241
|
/* Add up length of all initrd images */
|
186
|
242
|
for_each_image ( initrd ) {
|
187
|
|
- if ( initrd->type != &initrd_image_type )
|
188
|
|
- continue;
|
189
|
|
- initrd_len = ( ( initrd->len + 0x0f ) & ~0x0f );
|
190
|
|
- total_len += initrd_len;
|
|
243
|
+ total_len += bzimage_load_initrd ( image, initrd, UNULL );
|
191
|
244
|
}
|
192
|
245
|
|
193
|
246
|
/* Give up if no initrd images found */
|
|
@@ -198,47 +251,39 @@ static int bzimage_load_initrd ( struct image *image,
|
198
|
251
|
* starting from the downloaded kernel image itself and
|
199
|
252
|
* working downwards until we hit an available region.
|
200
|
253
|
*/
|
201
|
|
- for ( start = ( user_to_phys ( image->data, 0 ) & ~0xfffff ) ; ;
|
202
|
|
- start -= 0x100000 ) {
|
|
254
|
+ for ( address = ( user_to_phys ( image->data, 0 ) & ~0xfffff ) ; ;
|
|
255
|
+ address -= 0x100000 ) {
|
203
|
256
|
/* Check that we're not going to overwrite the
|
204
|
257
|
* kernel itself. This check isn't totally
|
205
|
258
|
* accurate, but errs on the side of caution.
|
206
|
259
|
*/
|
207
|
|
- if ( start <= ( BZI_LOAD_HIGH_ADDR + image->len ) ) {
|
|
260
|
+ if ( address <= ( BZI_LOAD_HIGH_ADDR + image->len ) ) {
|
208
|
261
|
DBGC ( image, "bzImage %p could not find a location "
|
209
|
262
|
"for initrd\n", image );
|
210
|
263
|
return -ENOBUFS;
|
211
|
264
|
}
|
212
|
265
|
/* Check that we are within the kernel's range */
|
213
|
|
- if ( ( start + total_len ) > exec_ctx->mem_limit )
|
|
266
|
+ if ( ( address + total_len ) > exec_ctx->mem_limit )
|
214
|
267
|
continue;
|
215
|
268
|
/* Prepare and verify segment */
|
216
|
|
- if ( ( rc = prep_segment ( phys_to_user ( start ), 0,
|
|
269
|
+ if ( ( rc = prep_segment ( phys_to_user ( address ), 0,
|
217
|
270
|
total_len ) ) != 0 )
|
218
|
271
|
continue;
|
219
|
272
|
/* Use this address */
|
220
|
273
|
break;
|
221
|
274
|
}
|
222
|
275
|
|
|
276
|
+ /* Record initrd location */
|
|
277
|
+ exec_ctx->ramdisk_image = address;
|
|
278
|
+ exec_ctx->ramdisk_size = total_len;
|
|
279
|
+
|
223
|
280
|
/* Construct initrd */
|
224
|
281
|
DBGC ( image, "bzImage %p constructing initrd at [%lx,%lx)\n",
|
225
|
|
- image, start, ( start + total_len ) );
|
|
282
|
+ image, address, ( address + total_len ) );
|
226
|
283
|
for_each_image ( initrd ) {
|
227
|
|
- if ( initrd->type != &initrd_image_type )
|
228
|
|
- continue;
|
229
|
|
- initrd_len = ( ( initrd->len + 0x0f ) & ~0x0f );
|
230
|
|
- DBGC ( image, "bzImage %p has initrd %p at [%lx,%lx)\n",
|
231
|
|
- image, initrd, ( start + offset ),
|
232
|
|
- ( start + offset + initrd->len ) );
|
233
|
|
- memcpy_user ( phys_to_user ( start ), offset,
|
234
|
|
- initrd->data, 0, initrd->len );
|
235
|
|
- offset += initrd_len;
|
|
284
|
+ address += bzimage_load_initrd ( image, initrd,
|
|
285
|
+ phys_to_user ( address ) );
|
236
|
286
|
}
|
237
|
|
- assert ( offset == total_len );
|
238
|
|
-
|
239
|
|
- /* Record initrd location */
|
240
|
|
- exec_ctx->ramdisk_image = start;
|
241
|
|
- exec_ctx->ramdisk_size = total_len;
|
242
|
287
|
|
243
|
288
|
return 0;
|
244
|
289
|
}
|
|
@@ -281,7 +326,7 @@ static int bzimage_exec ( struct image *image ) {
|
281
|
326
|
return rc;
|
282
|
327
|
|
283
|
328
|
/* Load any initrds */
|
284
|
|
- if ( ( rc = bzimage_load_initrd ( image, &exec_ctx ) ) != 0 )
|
|
329
|
+ if ( ( rc = bzimage_load_initrds ( image, &exec_ctx ) ) != 0 )
|
285
|
330
|
return rc;
|
286
|
331
|
|
287
|
332
|
/* Update and store kernel header */
|