blob: 9de9e59ba5c90992ad9c0b6887b6e1072916d0b8 [file] [log] [blame]
Randall Spangler3dcf9dc2010-06-02 12:46:17 -07001/* 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 */
30static LoadKernelParams lkp;
31static FILE *image_file = NULL;
32
33
34/* Boot device stub implementations to read from the image file */
35int BootDeviceReadLBA(uint64_t lba_start, uint64_t lba_count, void *buffer) {
Colin Chowafdd0582010-06-02 18:20:59 -070036 printf("Read(%llu, %llu)\n", lba_start, lba_count);
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070037
38 if (lba_start > lkp.ending_lba ||
39 lba_start + lba_count - 1 > lkp.ending_lba) {
Colin Chowafdd0582010-06-02 18:20:59 -070040 fprintf(stderr, "Read overrun: %llu + %llu > %llu\n",
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070041 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
54int BootDeviceWriteLBA(uint64_t lba_start, uint64_t lba_count,
55 const void *buffer) {
Colin Chowafdd0582010-06-02 18:20:59 -070056 printf("Write(%llu, %llu)\n", lba_start, lba_count);
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070057
58 if (lba_start > lkp.ending_lba ||
59 lba_start + lba_count - 1 > lkp.ending_lba) {
Colin Chowafdd0582010-06-02 18:20:59 -070060 fprintf(stderr, "Read overrun: %llu + %llu > %llu\n",
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070061 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 */
78int 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 Chowafdd0582010-06-02 18:20:59 -0700106 printf("Ending LBA: %llu\n", lkp.ending_lba);
Randall Spangler3dcf9dc2010-06-02 12:46:17 -0700107
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}