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