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