| 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 #include "kernel_image.h" | 8 #include "kernel_image.h" |
| 9 | 9 |
| 10 #include <fcntl.h> | 10 #include <fcntl.h> |
| (...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 int is_subkey_out) { | 326 int is_subkey_out) { |
| 327 int fd; | 327 int fd; |
| 328 int success = 1; | 328 int success = 1; |
| 329 uint8_t* kernel_blob = NULL; | 329 uint8_t* kernel_blob = NULL; |
| 330 uint8_t* subkey_out_buf = NULL; | 330 uint8_t* subkey_out_buf = NULL; |
| 331 uint8_t* subkey_header = NULL; | 331 uint8_t* subkey_header = NULL; |
| 332 uint64_t blob_len; | 332 uint64_t blob_len; |
| 333 | 333 |
| 334 if (!image) | 334 if (!image) |
| 335 return 0; | 335 return 0; |
| 336 if (-1 == (fd = creat(output_file, S_IRWXU))) { | 336 if (-1 == (fd = creat(output_file, 0666))) { |
| 337 debug("Couldn't open file for writing kernel image: %s\n", | 337 debug("Couldn't open file for writing kernel image: %s\n", |
| 338 output_file); | 338 output_file); |
| 339 return 0; | 339 return 0; |
| 340 } | 340 } |
| 341 if (is_subkey_out) { | 341 if (is_subkey_out) { |
| 342 blob_len = GetKernelHeaderLen(image) + | 342 blob_len = GetKernelHeaderLen(image) + |
| 343 siglen_map[image->firmware_sign_algorithm]; | 343 siglen_map[image->firmware_sign_algorithm]; |
| 344 subkey_out_buf = (uint8_t*) Malloc(blob_len); | 344 subkey_out_buf = (uint8_t*) Malloc(blob_len); |
| 345 subkey_header = GetKernelHeaderBlob(image); | 345 subkey_header = GetKernelHeaderBlob(image); |
| 346 Memcpy(subkey_out_buf, subkey_header, GetKernelHeaderLen(image)); | 346 Memcpy(subkey_out_buf, subkey_header, GetKernelHeaderLen(image)); |
| (...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 662 uint64_t i; | 662 uint64_t i; |
| 663 | 663 |
| 664 // Read the input files. | 664 // Read the input files. |
| 665 kernel_buf = BufferFromFile(kernel_file, &kernel_size); | 665 kernel_buf = BufferFromFile(kernel_file, &kernel_size); |
| 666 if (!kernel_buf) | 666 if (!kernel_buf) |
| 667 goto done0; | 667 goto done0; |
| 668 | 668 |
| 669 config_buf = BufferFromFile(config_file, &config_size); | 669 config_buf = BufferFromFile(config_file, &config_size); |
| 670 if (!config_buf) | 670 if (!config_buf) |
| 671 goto done1; | 671 goto done1; |
| 672 if (config_size < CROS_CONFIG_SIZE) // need room for trailing '\0' | 672 if (config_size >= CROS_CONFIG_SIZE) { // need room for trailing '\0' |
| 673 error("config file %s is too large (>= %d bytes)\n", |
| 674 config_file, CROS_CONFIG_SIZE); |
| 673 goto done1; | 675 goto done1; |
| 676 } |
| 674 | 677 |
| 675 // Replace any newlines with spaces in the config file. | 678 // Replace any newlines with spaces in the config file. |
| 676 for (i=0; i < config_size; i++) | 679 for (i=0; i < config_size; i++) |
| 677 if (config_buf[i] == '\n') | 680 if (config_buf[i] == '\n') |
| 678 config_buf[i] = ' '; | 681 config_buf[i] = ' '; |
| 679 | 682 |
| 680 bootloader_buf = BufferFromFile(bootloader_file, &bootloader_size); | 683 bootloader_buf = BufferFromFile(bootloader_file, &bootloader_size); |
| 681 if (!bootloader_buf) | 684 if (!bootloader_buf) |
| 682 goto done2; | 685 goto done2; |
| 683 | 686 |
| 684 // The first part of vmlinuz is a header, followed by a real-mode boot stub. | 687 // The first part of vmlinuz is a header, followed by a real-mode boot stub. |
| 685 // We only want the 32-bit part. | 688 // We only want the 32-bit part. |
| 686 if (kernel_size) { | 689 if (kernel_size) { |
| 687 lh = (struct linux_kernel_header *)kernel_buf; | 690 lh = (struct linux_kernel_header *)kernel_buf; |
| 688 kernel32_start = (lh->setup_sects+1) << 9; | 691 kernel32_start = (lh->setup_sects+1) << 9; |
| 689 kernel32_size = kernel_size - kernel32_start; | 692 kernel32_size = kernel_size - kernel32_start; |
| 690 } | 693 } |
| 691 | 694 |
| 692 // Allocate and zero the blob we need. | 695 // Allocate and zero the blob we need. |
| 693 blob_size = roundup(kernel32_size, CROS_ALIGN) + | 696 blob_size = roundup(kernel32_size, CROS_ALIGN) + |
| 694 CROS_CONFIG_SIZE + | 697 CROS_CONFIG_SIZE + |
| 695 CROS_PARAMS_SIZE + | 698 CROS_PARAMS_SIZE + |
| 696 roundup(bootloader_size, CROS_ALIGN); | 699 roundup(bootloader_size, CROS_ALIGN); |
| 697 blob = (uint8_t *)Malloc(blob_size); | 700 blob = (uint8_t *)Malloc(blob_size); |
| 698 if (!blob) | 701 if (!blob) { |
| 702 error("Couldn't allocate %ld bytes.\n", blob_size); |
| 699 goto done3; | 703 goto done3; |
| 704 } |
| 700 Memset(blob, 0, blob_size); | 705 Memset(blob, 0, blob_size); |
| 701 now = 0; | 706 now = 0; |
| 702 | 707 |
| 703 // Copy the 32-bit kernel. | 708 // Copy the 32-bit kernel. |
| 704 if (kernel32_size) | 709 if (kernel32_size) |
| 705 Memcpy(blob + now, kernel_buf + kernel32_start, kernel32_size); | 710 Memcpy(blob + now, kernel_buf + kernel32_start, kernel32_size); |
| 706 now += roundup(now + kernel32_size, CROS_ALIGN); | 711 now += roundup(now + kernel32_size, CROS_ALIGN); |
| 707 | 712 |
| 708 // Find the load address of the commandline. We'll need it later. | 713 // Find the load address of the commandline. We'll need it later. |
| 709 cmdline_addr = CROS_32BIT_ENTRY_ADDR + now | 714 cmdline_addr = CROS_32BIT_ENTRY_ADDR + now |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 746 // Clean up and return the blob. | 751 // Clean up and return the blob. |
| 747 done3: | 752 done3: |
| 748 Free(bootloader_buf); | 753 Free(bootloader_buf); |
| 749 done2: | 754 done2: |
| 750 Free(config_buf); | 755 Free(config_buf); |
| 751 done1: | 756 done1: |
| 752 Free(kernel_buf); | 757 Free(kernel_buf); |
| 753 done0: | 758 done0: |
| 754 return blob; | 759 return blob; |
| 755 } | 760 } |
| OLD | NEW |