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 <inttypes.h> /* For PRIu64 macro */ |
11 #include <stdio.h> | 11 #include <stdio.h> |
12 #include <stdlib.h> | 12 #include <stdlib.h> |
13 #include <string.h> | 13 #include <string.h> |
14 #include <sys/types.h> | 14 #include <sys/types.h> |
| 15 #include <unistd.h> |
15 | 16 |
16 #include "load_kernel_fw.h" | 17 #include "load_kernel_fw.h" |
17 #include "boot_device.h" | 18 #include "boot_device.h" |
18 #include "host_common.h" | 19 #include "host_common.h" |
19 #include "rollback_index.h" | 20 #include "rollback_index.h" |
20 #include "utility.h" | 21 #include "utility.h" |
21 #include "vboot_kernel.h" | 22 #include "vboot_kernel.h" |
22 | 23 |
23 #define LBA_BYTES 512 | 24 #define LBA_BYTES 512 |
24 #define KERNEL_BUFFER_SIZE 0xA00000 | 25 #define KERNEL_BUFFER_SIZE 0xA00000 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 return 1; | 69 return 1; |
69 } | 70 } |
70 return 0; | 71 return 0; |
71 } | 72 } |
72 | 73 |
73 | 74 |
74 /* Main routine */ | 75 /* Main routine */ |
75 int main(int argc, char* argv[]) { | 76 int main(int argc, char* argv[]) { |
76 | 77 |
77 const char* image_name; | 78 const char* image_name; |
78 const char* keyfile_name; | 79 int rv, c, argsleft; |
79 int rv; | 80 int errorcnt = 0; |
| 81 char *e = 0; |
80 | 82 |
81 Memset(&lkp, 0, sizeof(LoadKernelParams)); | 83 Memset(&lkp, 0, sizeof(LoadKernelParams)); |
82 lkp.bytes_per_lba = LBA_BYTES; | 84 lkp.bytes_per_lba = LBA_BYTES; |
| 85 lkp.boot_flags = BOOT_FLAG_RECOVERY; |
83 | 86 |
84 /* Read command line parameters */ | 87 /* Parse options */ |
85 if (3 > argc) { | 88 opterr = 0; |
86 fprintf(stderr, "usage: %s <drive_image> <sign_key> [boot flag]\n", argv[0])
; | 89 while ((c=getopt(argc, argv, ":b:")) != -1) |
| 90 { |
| 91 switch (c) |
| 92 { |
| 93 case 'b': |
| 94 lkp.boot_flags = strtoull(optarg, &e, 0); |
| 95 if (!*optarg || (e && *e)) |
| 96 { |
| 97 fprintf(stderr, "Invalid argument to -%c: \"%s\"\n", c, optarg); |
| 98 errorcnt++; |
| 99 } |
| 100 break; |
| 101 case '?': |
| 102 fprintf(stderr, "Unrecognized switch: -%c\n", optopt); |
| 103 errorcnt++; |
| 104 break; |
| 105 case ':': |
| 106 fprintf(stderr, "Missing argument to -%c\n", optopt); |
| 107 errorcnt++; |
| 108 break; |
| 109 default: |
| 110 errorcnt++; |
| 111 break; |
| 112 } |
| 113 } |
| 114 |
| 115 /* Update argc */ |
| 116 argsleft = argc - optind; |
| 117 |
| 118 if (errorcnt || !argsleft) |
| 119 { |
| 120 fprintf(stderr, "usage: %s [options] <drive_image> [<sign_key>]\n", |
| 121 argv[0]); |
| 122 fprintf(stderr, "\noptions:\n"); |
| 123 fprintf(stderr, " -b NUM boot flag bits (default %" PRIu64 "):\n", |
| 124 BOOT_FLAG_RECOVERY); |
| 125 fprintf(stderr, " %" PRIu64 " = developer mode on\n", |
| 126 BOOT_FLAG_DEVELOPER); |
| 127 fprintf(stderr, " %" PRIu64 " = recovery mode on\n", |
| 128 BOOT_FLAG_RECOVERY); |
87 return 1; | 129 return 1; |
88 } | 130 } |
89 image_name = argv[1]; | 131 |
90 keyfile_name = argv[2]; | 132 image_name = argv[optind]; |
91 | 133 |
92 /* Read header signing key blob */ | 134 /* Read header signing key blob */ |
93 { | 135 if (argsleft > 1) { |
94 uint64_t key_size; | 136 uint64_t key_size; |
95 lkp.header_sign_key_blob = ReadFile(keyfile_name, &key_size); | 137 lkp.header_sign_key_blob = ReadFile(argv[optind+1], &key_size); |
96 if (!lkp.header_sign_key_blob) { | 138 if (!lkp.header_sign_key_blob) { |
97 fprintf(stderr, "Unable to read key file %s\n", keyfile_name); | 139 fprintf(stderr, "Unable to read key file %s\n", argv[optind+1]); |
98 return 1; | 140 return 1; |
99 } | 141 } |
100 } | 142 } |
| 143 /* Need to skip the address check, since we're putting it somewhere on the |
| 144 * heap instead of its actual target address in the firmware. */ |
| 145 lkp.boot_flags |= BOOT_FLAG_SKIP_ADDR_CHECK; |
| 146 |
| 147 printf("bootflags = %" PRIu64 "\n", lkp.boot_flags); |
101 | 148 |
102 /* Get image size */ | 149 /* Get image size */ |
103 printf("Reading from image: %s\n", image_name); | 150 printf("Reading from image: %s\n", image_name); |
104 image_file = fopen(image_name, "rb"); | 151 image_file = fopen(image_name, "rb"); |
105 if (!image_file) { | 152 if (!image_file) { |
106 fprintf(stderr, "Unable to open image file %s\n", image_name); | 153 fprintf(stderr, "Unable to open image file %s\n", image_name); |
107 return 1; | 154 return 1; |
108 } | 155 } |
109 fseek(image_file, 0, SEEK_END); | 156 fseek(image_file, 0, SEEK_END); |
110 lkp.ending_lba = (ftell(image_file) / LBA_BYTES) - 1; | 157 lkp.ending_lba = (ftell(image_file) / LBA_BYTES) - 1; |
111 rewind(image_file); | 158 rewind(image_file); |
112 printf("Ending LBA: %" PRIu64 "\n", lkp.ending_lba); | 159 printf("Ending LBA: %" PRIu64 "\n", lkp.ending_lba); |
113 | 160 |
114 /* Allocate a buffer for the kernel */ | 161 /* Allocate a buffer for the kernel */ |
115 lkp.kernel_buffer = Malloc(KERNEL_BUFFER_SIZE); | 162 lkp.kernel_buffer = Malloc(KERNEL_BUFFER_SIZE); |
116 if(!lkp.kernel_buffer) { | 163 if(!lkp.kernel_buffer) { |
117 fprintf(stderr, "Unable to allocate kernel buffer.\n"); | 164 fprintf(stderr, "Unable to allocate kernel buffer.\n"); |
118 return 1; | 165 return 1; |
119 } | 166 } |
120 lkp.kernel_buffer_size = KERNEL_BUFFER_SIZE; | 167 lkp.kernel_buffer_size = KERNEL_BUFFER_SIZE; |
121 | 168 |
122 /* Need to skip the address check, since we're putting it somewhere on the | |
123 * heap instead of its actual target address in the firmware. */ | |
124 if (argc == 4) { | |
125 lkp.boot_flags = atoi(argv[3]) | BOOT_FLAG_SKIP_ADDR_CHECK; | |
126 } else { | |
127 /* Default to recovery. */ | |
128 lkp.boot_flags = BOOT_FLAG_SKIP_ADDR_CHECK | BOOT_FLAG_RECOVERY; | |
129 } | |
130 /* Call LoadKernel() */ | 169 /* Call LoadKernel() */ |
131 rv = LoadKernel(&lkp); | 170 rv = LoadKernel(&lkp); |
132 printf("LoadKernel() returned %d\n", rv); | 171 printf("LoadKernel() returned %d\n", rv); |
133 | 172 |
134 if (LOAD_KERNEL_SUCCESS == rv) { | 173 if (LOAD_KERNEL_SUCCESS == rv) { |
135 printf("Partition number: %" PRIu64 "\n", lkp.partition_number); | 174 printf("Partition number: %" PRIu64 "\n", lkp.partition_number); |
136 printf("Bootloader address: %" PRIu64 "\n", lkp.bootloader_address); | 175 printf("Bootloader address: %" PRIu64 "\n", lkp.bootloader_address); |
137 printf("Bootloader size: %" PRIu64 "\n", lkp.bootloader_size); | 176 printf("Bootloader size: %" PRIu64 "\n", lkp.bootloader_size); |
138 printf("Partition guid: " | 177 printf("Partition guid: " |
139 "%02x%02x%02x%02x-%02x%02x-%02x%02x" | 178 "%02x%02x%02x%02x-%02x%02x-%02x%02x" |
(...skipping 13 matching lines...) Expand all Loading... |
153 lkp.partition_guid[12], | 192 lkp.partition_guid[12], |
154 lkp.partition_guid[13], | 193 lkp.partition_guid[13], |
155 lkp.partition_guid[14], | 194 lkp.partition_guid[14], |
156 lkp.partition_guid[15]); | 195 lkp.partition_guid[15]); |
157 } | 196 } |
158 | 197 |
159 fclose(image_file); | 198 fclose(image_file); |
160 Free(lkp.kernel_buffer); | 199 Free(lkp.kernel_buffer); |
161 return rv != LOAD_KERNEL_SUCCESS; | 200 return rv != LOAD_KERNEL_SUCCESS; |
162 } | 201 } |
OLD | NEW |