Randall Spangler | 3dcf9dc | 2010-06-02 12:46:17 -0700 | [diff] [blame] | 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 <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <sys/types.h> |
| 14 | |
| 15 | #include "load_kernel_fw.h" |
| 16 | #include "boot_device.h" |
| 17 | #include "rollback_index.h" |
| 18 | #include "utility.h" |
| 19 | |
| 20 | /* ANSI Color coding sequences. */ |
| 21 | #define COL_GREEN "\e[1;32m" |
| 22 | #define COL_RED "\e[0;31m" |
| 23 | #define COL_STOP "\e[m" |
| 24 | |
| 25 | |
| 26 | #define LBA_BYTES 512 |
| 27 | #define KERNEL_BUFFER_SIZE 0x600000 |
| 28 | |
| 29 | /* Global variables for stub functions */ |
| 30 | static LoadKernelParams lkp; |
| 31 | static FILE *image_file = NULL; |
| 32 | |
| 33 | |
| 34 | /* Boot device stub implementations to read from the image file */ |
| 35 | int BootDeviceReadLBA(uint64_t lba_start, uint64_t lba_count, void *buffer) { |
Colin Chow | afdd058 | 2010-06-02 18:20:59 -0700 | [diff] [blame] | 36 | printf("Read(%llu, %llu)\n", lba_start, lba_count); |
Randall Spangler | 3dcf9dc | 2010-06-02 12:46:17 -0700 | [diff] [blame] | 37 | |
| 38 | if (lba_start > lkp.ending_lba || |
| 39 | lba_start + lba_count - 1 > lkp.ending_lba) { |
Colin Chow | afdd058 | 2010-06-02 18:20:59 -0700 | [diff] [blame] | 40 | fprintf(stderr, "Read overrun: %llu + %llu > %llu\n", |
Randall Spangler | 3dcf9dc | 2010-06-02 12:46:17 -0700 | [diff] [blame] | 41 | lba_start, lba_count, lkp.ending_lba); |
| 42 | return 1; |
| 43 | } |
| 44 | |
| 45 | fseek(image_file, lba_start * lkp.bytes_per_lba, SEEK_SET); |
| 46 | if (1 != fread(buffer, lba_count * lkp.bytes_per_lba, 1, image_file)) { |
| 47 | fprintf(stderr, "Read error."); |
| 48 | return 1; |
| 49 | } |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | |
| 54 | int BootDeviceWriteLBA(uint64_t lba_start, uint64_t lba_count, |
| 55 | const void *buffer) { |
Colin Chow | afdd058 | 2010-06-02 18:20:59 -0700 | [diff] [blame] | 56 | printf("Write(%llu, %llu)\n", lba_start, lba_count); |
Randall Spangler | 3dcf9dc | 2010-06-02 12:46:17 -0700 | [diff] [blame] | 57 | |
| 58 | if (lba_start > lkp.ending_lba || |
| 59 | lba_start + lba_count - 1 > lkp.ending_lba) { |
Colin Chow | afdd058 | 2010-06-02 18:20:59 -0700 | [diff] [blame] | 60 | fprintf(stderr, "Read overrun: %llu + %llu > %llu\n", |
Randall Spangler | 3dcf9dc | 2010-06-02 12:46:17 -0700 | [diff] [blame] | 61 | lba_start, lba_count, lkp.ending_lba); |
| 62 | return 1; |
| 63 | } |
| 64 | |
| 65 | /* TODO: enable writes, once we're sure it won't trash our example file */ |
| 66 | return 0; |
| 67 | |
| 68 | fseek(image_file, lba_start * lkp.bytes_per_lba, SEEK_SET); |
| 69 | if (1 != fwrite(buffer, lba_count * lkp.bytes_per_lba, 1, image_file)) { |
| 70 | fprintf(stderr, "Read error."); |
| 71 | return 1; |
| 72 | } |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | /* Main routine */ |
| 78 | int main(int argc, char* argv[]) { |
| 79 | |
| 80 | const char *image_name; |
| 81 | int rv; |
| 82 | |
| 83 | Memset(&lkp, 0, sizeof(LoadKernelParams)); |
| 84 | lkp.bytes_per_lba = LBA_BYTES; |
| 85 | |
| 86 | /* Read command line parameters */ |
| 87 | if (2 > argc) { |
| 88 | fprintf(stderr, "usage: %s <drive_image>\n", argv[0]); |
| 89 | return 1; |
| 90 | } |
| 91 | image_name = argv[1]; |
| 92 | printf("Reading from image: %s\n", image_name); |
| 93 | |
| 94 | /* TODO: Read header signing key blob */ |
| 95 | lkp.header_sign_key_blob = NULL; |
| 96 | |
| 97 | /* Get image size */ |
| 98 | image_file = fopen(image_name, "rb"); |
| 99 | if (!image_file) { |
| 100 | fprintf(stderr, "Unable to open image file %s\n", image_name); |
| 101 | return 1; |
| 102 | } |
| 103 | fseek(image_file, 0, SEEK_END); |
| 104 | lkp.ending_lba = (ftell(image_file) / LBA_BYTES) - 1; |
| 105 | rewind(image_file); |
Colin Chow | afdd058 | 2010-06-02 18:20:59 -0700 | [diff] [blame] | 106 | printf("Ending LBA: %llu\n", lkp.ending_lba); |
Randall Spangler | 3dcf9dc | 2010-06-02 12:46:17 -0700 | [diff] [blame] | 107 | |
| 108 | /* Allocate a buffer for the kernel */ |
| 109 | lkp.kernel_buffer = Malloc(KERNEL_BUFFER_SIZE); |
| 110 | if(!lkp.kernel_buffer) { |
| 111 | fprintf(stderr, "Unable to allocate kernel buffer.\n"); |
| 112 | return 1; |
| 113 | } |
| 114 | |
| 115 | /* TODO: Option for boot mode */ |
| 116 | lkp.boot_mode = BOOT_MODE_NORMAL; |
| 117 | |
| 118 | /* Call LoadKernel() */ |
| 119 | rv = LoadKernel(&lkp); |
| 120 | printf("LoadKernel() returned %d\n", rv); |
| 121 | |
| 122 | fclose(image_file); |
| 123 | Free(lkp.kernel_buffer); |
| 124 | return 0; |
| 125 | } |