OLD | NEW |
(Empty) | |
| 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 |
| 3 * found in the LICENSE file. |
| 4 */ |
| 5 |
| 6 /* Routines for verifying a file's signature. Useful in testing the core |
| 7 * RSA verification implementation. |
| 8 */ |
| 9 |
| 10 #include <inttypes.h> /* For PRIu64 macro */ |
| 11 #include <stdio.h> |
| 12 #include <stdlib.h> |
| 13 #include <string.h> |
| 14 #include <sys/types.h> |
| 15 |
| 16 #include "load_kernel_fw.h" |
| 17 #include "boot_device.h" |
| 18 #include "host_common.h" |
| 19 #include "rollback_index.h" |
| 20 #include "utility.h" |
| 21 #include "vboot_kernel.h" |
| 22 |
| 23 /* ANSI Color coding sequences. */ |
| 24 #define COL_GREEN "\e[1;32m" |
| 25 #define COL_RED "\e[0;31m" |
| 26 #define COL_STOP "\e[m" |
| 27 |
| 28 |
| 29 #define LBA_BYTES 512 |
| 30 #define KERNEL_BUFFER_SIZE 0x600000 |
| 31 |
| 32 /* Global variables for stub functions */ |
| 33 static LoadKernelParams lkp; |
| 34 static FILE *image_file = NULL; |
| 35 |
| 36 |
| 37 /* Boot device stub implementations to read from the image file */ |
| 38 int BootDeviceReadLBA(uint64_t lba_start, uint64_t lba_count, void *buffer) { |
| 39 printf("Read(%" PRIu64 ", %" PRIu64 ")\n", lba_start, lba_count); |
| 40 |
| 41 if (lba_start > lkp.ending_lba || |
| 42 lba_start + lba_count - 1 > lkp.ending_lba) { |
| 43 fprintf(stderr, "Read overrun: %" PRIu64 " + %" PRIu64 " > %" PRIu64 "\n", |
| 44 lba_start, lba_count, lkp.ending_lba); |
| 45 return 1; |
| 46 } |
| 47 |
| 48 fseek(image_file, lba_start * lkp.bytes_per_lba, SEEK_SET); |
| 49 if (1 != fread(buffer, lba_count * lkp.bytes_per_lba, 1, image_file)) { |
| 50 fprintf(stderr, "Read error."); |
| 51 return 1; |
| 52 } |
| 53 return 0; |
| 54 } |
| 55 |
| 56 |
| 57 int BootDeviceWriteLBA(uint64_t lba_start, uint64_t lba_count, |
| 58 const void *buffer) { |
| 59 printf("Write(%" PRIu64 ", %" PRIu64 ")\n", lba_start, lba_count); |
| 60 |
| 61 if (lba_start > lkp.ending_lba || |
| 62 lba_start + lba_count - 1 > lkp.ending_lba) { |
| 63 fprintf(stderr, "Read overrun: %" PRIu64 " + %" PRIu64 " > %" PRIu64 "\n", |
| 64 lba_start, lba_count, lkp.ending_lba); |
| 65 return 1; |
| 66 } |
| 67 |
| 68 /* TODO: enable writes, once we're sure it won't trash our example file */ |
| 69 return 0; |
| 70 |
| 71 fseek(image_file, lba_start * lkp.bytes_per_lba, SEEK_SET); |
| 72 if (1 != fwrite(buffer, lba_count * lkp.bytes_per_lba, 1, image_file)) { |
| 73 fprintf(stderr, "Read error."); |
| 74 return 1; |
| 75 } |
| 76 return 0; |
| 77 } |
| 78 |
| 79 |
| 80 /* Main routine */ |
| 81 int main(int argc, char* argv[]) { |
| 82 |
| 83 const char* image_name; |
| 84 const char* keyfile_name; |
| 85 int rv; |
| 86 |
| 87 Memset(&lkp, 0, sizeof(LoadKernelParams)); |
| 88 lkp.bytes_per_lba = LBA_BYTES; |
| 89 |
| 90 /* Read command line parameters */ |
| 91 if (3 > argc) { |
| 92 fprintf(stderr, "usage: %s <drive_image> <sign_key>\n", argv[0]); |
| 93 return 1; |
| 94 } |
| 95 image_name = argv[1]; |
| 96 keyfile_name = argv[2]; |
| 97 |
| 98 /* Read header signing key blob */ |
| 99 { |
| 100 uint64_t key_size; |
| 101 lkp.header_sign_key_blob = ReadFile(keyfile_name, &key_size); |
| 102 if (!lkp.header_sign_key_blob) { |
| 103 fprintf(stderr, "Unable to read key file %s\n", keyfile_name); |
| 104 return 1; |
| 105 } |
| 106 } |
| 107 |
| 108 /* Get image size */ |
| 109 printf("Reading from image: %s\n", image_name); |
| 110 image_file = fopen(image_name, "rb"); |
| 111 if (!image_file) { |
| 112 fprintf(stderr, "Unable to open image file %s\n", image_name); |
| 113 return 1; |
| 114 } |
| 115 fseek(image_file, 0, SEEK_END); |
| 116 lkp.ending_lba = (ftell(image_file) / LBA_BYTES) - 1; |
| 117 rewind(image_file); |
| 118 printf("Ending LBA: %" PRIu64 "\n", lkp.ending_lba); |
| 119 |
| 120 /* Allocate a buffer for the kernel */ |
| 121 lkp.kernel_buffer = Malloc(KERNEL_BUFFER_SIZE); |
| 122 if(!lkp.kernel_buffer) { |
| 123 fprintf(stderr, "Unable to allocate kernel buffer.\n"); |
| 124 return 1; |
| 125 } |
| 126 |
| 127 /* TODO: Option for boot mode - developer, recovery */ |
| 128 /* Need to skip the address check, since we're putting it somewhere on the |
| 129 * heap instead of its actual target address in the firmware. */ |
| 130 lkp.boot_flags = BOOT_FLAG_SKIP_ADDR_CHECK; |
| 131 |
| 132 /* Call LoadKernel() */ |
| 133 rv = LoadKernel2(&lkp); |
| 134 printf("LoadKernel() returned %d\n", rv); |
| 135 |
| 136 if (LOAD_KERNEL_SUCCESS == rv) { |
| 137 printf("Partition number: %" PRIu64 "\n", lkp.partition_number); |
| 138 printf("Bootloader address: %" PRIu64 "\n", lkp.bootloader_address); |
| 139 printf("Bootloader size: %" PRIu64 "\n", lkp.bootloader_size); |
| 140 } |
| 141 |
| 142 fclose(image_file); |
| 143 Free(lkp.kernel_buffer); |
| 144 return 0; |
| 145 } |
OLD | NEW |