| OLD | NEW |
| 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
| 4 * | 4 * |
| 5 * Functions for generating and manipulating a verified boot kernel image. | 5 * Functions for generating and manipulating a verified boot kernel image. |
| 6 * (Userland portion) | 6 * (Userland portion) |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 #include "kernel_image.h" | 9 #include "kernel_image.h" |
| 10 | 10 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 | 22 |
| 23 /* Macro to determine the size of a field structure in the KernelImage | 23 /* Macro to determine the size of a field structure in the KernelImage |
| 24 * structure. */ | 24 * structure. */ |
| 25 #define FIELD_LEN(field) (sizeof(((KernelImage*)0)->field)) | 25 #define FIELD_LEN(field) (sizeof(((KernelImage*)0)->field)) |
| 26 | 26 |
| 27 KernelImage* KernelImageNew(void) { | 27 KernelImage* KernelImageNew(void) { |
| 28 KernelImage* image = (KernelImage*) Malloc(sizeof(KernelImage)); | 28 KernelImage* image = (KernelImage*) Malloc(sizeof(KernelImage)); |
| 29 if (image) { | 29 if (image) { |
| 30 image->kernel_sign_key = NULL; | 30 image->kernel_sign_key = NULL; |
| 31 image->kernel_key_signature = NULL; | 31 image->kernel_key_signature = NULL; |
| 32 Memset(image->options.cmd_line, 0, sizeof(image->options.cmd_line)); |
| 32 image->config_signature = NULL; | 33 image->config_signature = NULL; |
| 33 image->kernel_signature = NULL; | 34 image->kernel_signature = NULL; |
| 34 image->kernel_data = NULL; | 35 image->kernel_data = NULL; |
| 35 } | 36 } |
| 36 return image; | 37 return image; |
| 37 } | 38 } |
| 38 | 39 |
| 39 void KernelImageFree(KernelImage* image) { | 40 void KernelImageFree(KernelImage* image) { |
| 40 if (image) { | 41 if (image) { |
| 41 Free(image->kernel_sign_key); | 42 Free(image->kernel_sign_key); |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 Free(header_blob); | 292 Free(header_blob); |
| 292 | 293 |
| 293 if (st.overrun || st.remaining_len != 0) { /* Underrun or Overrun. */ | 294 if (st.overrun || st.remaining_len != 0) { /* Underrun or Overrun. */ |
| 294 Free(kernel_blob); | 295 Free(kernel_blob); |
| 295 return NULL; | 296 return NULL; |
| 296 } | 297 } |
| 297 return kernel_blob; | 298 return kernel_blob; |
| 298 } | 299 } |
| 299 | 300 |
| 300 int WriteKernelImage(const char* input_file, | 301 int WriteKernelImage(const char* input_file, |
| 301 const KernelImage* image) { | 302 const KernelImage* image, |
| 303 int is_only_vblock) { |
| 302 int fd; | 304 int fd; |
| 305 int success = 1; |
| 303 uint8_t* kernel_blob; | 306 uint8_t* kernel_blob; |
| 304 uint64_t blob_len; | 307 uint64_t blob_len; |
| 305 | 308 |
| 306 if (!image) | 309 if (!image) |
| 307 return 0; | 310 return 0; |
| 308 if (-1 == (fd = creat(input_file, S_IRWXU))) { | 311 if (-1 == (fd = creat(input_file, S_IRWXU))) { |
| 309 debug("Couldn't open file for writing kernel image: %s\n", | 312 debug("Couldn't open file for writing kernel image: %s\n", |
| 310 input_file); | 313 input_file); |
| 311 return 0; | 314 return 0; |
| 312 } | 315 } |
| 313 kernel_blob = GetKernelBlob(image, &blob_len); | 316 kernel_blob = GetKernelBlob(image, &blob_len); |
| 314 if (!kernel_blob) { | 317 if (!kernel_blob) { |
| 315 debug("Couldn't create kernel blob from KernelImage.\n"); | 318 debug("Couldn't create kernel blob from KernelImage.\n"); |
| 316 return 0; | 319 return 0; |
| 317 } | 320 } |
| 318 if (blob_len != write(fd, kernel_blob, blob_len)) { | 321 if (!is_only_vblock) { |
| 319 debug("Couldn't write Kernel Image to file: %s\n", | 322 if (blob_len != write(fd, kernel_blob, blob_len)) { |
| 323 debug("Couldn't write Kernel Image to file: %s\n", |
| 320 input_file); | 324 input_file); |
| 321 | 325 success = 0; |
| 322 Free(kernel_blob); | 326 } |
| 323 close(fd); | 327 } else { |
| 324 return 0; | 328 /* Exclude the kernel_data. */ |
| 329 int vblock_len = blob_len - image->options.kernel_len; |
| 330 if (vblock_len != write(fd, kernel_blob, vblock_len)) { |
| 331 debug("Couldn't write Kernel Image Verification block to file: %s\n", |
| 332 input_file); |
| 333 success = 0; |
| 334 } |
| 325 } | 335 } |
| 326 Free(kernel_blob); | 336 Free(kernel_blob); |
| 327 close(fd); | 337 close(fd); |
| 328 return 1; | 338 return success; |
| 329 } | 339 } |
| 330 | 340 |
| 331 void PrintKernelImage(const KernelImage* image) { | 341 void PrintKernelImage(const KernelImage* image) { |
| 332 if (!image) | 342 if (!image) |
| 333 return; | 343 return; |
| 334 | 344 |
| 335 /* Print header. */ | 345 /* Print header. */ |
| 336 printf("Header Version = %d\n" | 346 printf("Header Version = %d\n" |
| 337 "Header Length = %d\n" | 347 "Header Length = %d\n" |
| 338 "Kernel Key Signature Algorithm = %s\n" | 348 "Kernel Key Signature Algorithm = %s\n" |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 547 Free(kernel_buf); | 557 Free(kernel_buf); |
| 548 Free(config_blob); | 558 Free(config_blob); |
| 549 return 1; | 559 return 1; |
| 550 } | 560 } |
| 551 | 561 |
| 552 void PrintKernelEntry(kernel_entry* entry) { | 562 void PrintKernelEntry(kernel_entry* entry) { |
| 553 debug("Boot Priority = %d\n", entry->boot_priority); | 563 debug("Boot Priority = %d\n", entry->boot_priority); |
| 554 debug("Boot Tries Remaining = %d\n", entry->boot_tries_remaining); | 564 debug("Boot Tries Remaining = %d\n", entry->boot_tries_remaining); |
| 555 debug("Boot Success Flag = %d\n", entry->boot_success_flag); | 565 debug("Boot Success Flag = %d\n", entry->boot_success_flag); |
| 556 } | 566 } |
| OLD | NEW |