Gaurav Shah | 431b988 | 2010-02-12 15:54:37 -0800 | [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 | * |
Gaurav Shah | ed9c96a | 2010-03-30 18:56:07 -0700 | [diff] [blame] | 5 | * API definitions for a verified boot firmware image. |
| 6 | * (Userland Portion) |
Gaurav Shah | 431b988 | 2010-02-12 15:54:37 -0800 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #ifndef VBOOT_REFERENCE_FIRMWARE_IMAGE_H_ |
| 10 | #define VBOOT_REFERENCE_FIRMWARE_IMAGE_H_ |
| 11 | |
Gaurav Shah | ed9c96a | 2010-03-30 18:56:07 -0700 | [diff] [blame] | 12 | #include "firmware_image_fw.h" |
Gaurav Shah | 431b988 | 2010-02-12 15:54:37 -0800 | [diff] [blame] | 13 | |
| 14 | /* Allocate and return a new FirmwareImage structure. */ |
| 15 | FirmwareImage* FirmwareImageNew(void); |
| 16 | |
| 17 | /* Deep free the contents of [fw]. */ |
| 18 | void FirmwareImageFree(FirmwareImage* fw); |
| 19 | |
Gaurav Shah | f5564fa | 2010-03-02 15:40:01 -0800 | [diff] [blame] | 20 | /* Read firmware data from file named [input_file]. |
Gaurav Shah | 431b988 | 2010-02-12 15:54:37 -0800 | [diff] [blame] | 21 | * |
Gaurav Shah | 23a2f3a | 2010-02-26 15:09:43 -0800 | [diff] [blame] | 22 | * Returns a filled up FirmwareImage structure on success, NULL on error. |
Gaurav Shah | 431b988 | 2010-02-12 15:54:37 -0800 | [diff] [blame] | 23 | */ |
Gaurav Shah | 23a2f3a | 2010-02-26 15:09:43 -0800 | [diff] [blame] | 24 | FirmwareImage* ReadFirmwareImage(const char* input_file); |
Gaurav Shah | 431b988 | 2010-02-12 15:54:37 -0800 | [diff] [blame] | 25 | |
Gaurav Shah | 80d129b | 2010-03-03 17:58:43 -0800 | [diff] [blame] | 26 | /* Get the length of the header for image [image]. */ |
| 27 | int GetFirmwareHeaderLen(const FirmwareImage* image); |
| 28 | |
Gaurav Shah | 528a2c1 | 2010-03-18 13:10:10 -0700 | [diff] [blame] | 29 | /* Calculate and store the firmware header checksum of [image] |
| 30 | * in [header_checksum]. |
| 31 | * |
| 32 | * [header_checksum] must be a valid pointer to a buffer of |
| 33 | * SHA512_DIGEST_SIZE. |
| 34 | */ |
| 35 | void CalculateFirmwareHeaderChecksum(const FirmwareImage *image, |
| 36 | uint8_t* header_checksum); |
| 37 | |
Gaurav Shah | f5564fa | 2010-03-02 15:40:01 -0800 | [diff] [blame] | 38 | /* Get firmware header binary blob from an [image]. |
| 39 | * |
| 40 | * Caller owns the returned pointer and must Free() it. |
Gaurav Shah | 431b988 | 2010-02-12 15:54:37 -0800 | [diff] [blame] | 41 | */ |
Gaurav Shah | f5564fa | 2010-03-02 15:40:01 -0800 | [diff] [blame] | 42 | uint8_t* GetFirmwareHeaderBlob(const FirmwareImage* image); |
Gaurav Shah | 431b988 | 2010-02-12 15:54:37 -0800 | [diff] [blame] | 43 | |
Gaurav Shah | f5564fa | 2010-03-02 15:40:01 -0800 | [diff] [blame] | 44 | /* Get firmware preamble binary blob from an [image]. |
| 45 | * |
| 46 | * Caller owns the returned pointer and must Free() it. |
Gaurav Shah | 431b988 | 2010-02-12 15:54:37 -0800 | [diff] [blame] | 47 | */ |
Gaurav Shah | f5564fa | 2010-03-02 15:40:01 -0800 | [diff] [blame] | 48 | uint8_t* GetFirmwarePreambleBlob(const FirmwareImage* image); |
Gaurav Shah | 431b988 | 2010-02-12 15:54:37 -0800 | [diff] [blame] | 49 | |
Gaurav Shah | f5564fa | 2010-03-02 15:40:01 -0800 | [diff] [blame] | 50 | /* Get a verified firmware binary blob from an [image] and fill its |
| 51 | * length into blob_len. |
| 52 | * |
| 53 | * Caller owns the returned pointer and must Free() it. |
| 54 | */ |
Gaurav Shah | 456678b | 2010-03-10 18:38:45 -0800 | [diff] [blame] | 55 | uint8_t* GetFirmwareBlob(const FirmwareImage* image, uint64_t* blob_len); |
Gaurav Shah | 431b988 | 2010-02-12 15:54:37 -0800 | [diff] [blame] | 56 | |
| 57 | /* Write firmware data from [image] into a file named [input_file]. |
| 58 | * |
Gaurav Shah | 67660cd | 2010-05-29 01:58:07 -0700 | [diff] [blame] | 59 | * If [is_just_vblock] is non-zero, only the verification block (excluding the |
| 60 | * actual firmware_data) is output. |
| 61 | * if [is_subkey_out] is non-zero, only the firmware key verification (subkey) |
| 62 | * header is output. |
| 63 | * |
Gaurav Shah | f5564fa | 2010-03-02 15:40:01 -0800 | [diff] [blame] | 64 | * Return 1 on success, 0 on failure. |
Gaurav Shah | 431b988 | 2010-02-12 15:54:37 -0800 | [diff] [blame] | 65 | */ |
Gaurav Shah | f5564fa | 2010-03-02 15:40:01 -0800 | [diff] [blame] | 66 | int WriteFirmwareImage(const char* input_file, |
Gaurav Shah | 65127cc | 2010-04-14 14:29:02 -0700 | [diff] [blame] | 67 | const FirmwareImage* image, |
Gaurav Shah | 67660cd | 2010-05-29 01:58:07 -0700 | [diff] [blame] | 68 | int is_only_vblock, |
| 69 | int is_subkey_out); |
Gaurav Shah | f5564fa | 2010-03-02 15:40:01 -0800 | [diff] [blame] | 70 | |
Gaurav Shah | 431b988 | 2010-02-12 15:54:37 -0800 | [diff] [blame] | 71 | /* Pretty print the contents of [image]. Only headers and metadata information |
| 72 | * is printed. |
| 73 | */ |
Gaurav Shah | 08df9b8 | 2010-02-23 16:16:23 -0800 | [diff] [blame] | 74 | void PrintFirmwareImage(const FirmwareImage* image); |
| 75 | |
Gaurav Shah | 3199eed | 2010-03-25 13:04:45 -0700 | [diff] [blame] | 76 | /* Performs a chained verify of the firmware [image]. |
Gaurav Shah | 431b988 | 2010-02-12 15:54:37 -0800 | [diff] [blame] | 77 | * |
| 78 | * Returns 0 on success, error code on failure. |
| 79 | */ |
Gaurav Shah | 08df9b8 | 2010-02-23 16:16:23 -0800 | [diff] [blame] | 80 | int VerifyFirmwareImage(const RSAPublicKey* root_key, |
Gaurav Shah | 3199eed | 2010-03-25 13:04:45 -0700 | [diff] [blame] | 81 | const FirmwareImage* image); |
Gaurav Shah | 431b988 | 2010-02-12 15:54:37 -0800 | [diff] [blame] | 82 | |
| 83 | /* Maps error codes from VerifyFirmware() to error description. */ |
Gaurav Shah | 23a2f3a | 2010-02-26 15:09:43 -0800 | [diff] [blame] | 84 | const char* VerifyFirmwareErrorString(int error); |
Gaurav Shah | 431b988 | 2010-02-12 15:54:37 -0800 | [diff] [blame] | 85 | |
Gaurav Shah | 431b988 | 2010-02-12 15:54:37 -0800 | [diff] [blame] | 86 | /* Add a root key signature to the key header to a firmware image [image] |
| 87 | * using the private root key in file [root_key_file]. |
| 88 | * |
| 89 | * Return 1 on success, 0 on failure. |
| 90 | */ |
Gaurav Shah | 23a2f3a | 2010-02-26 15:09:43 -0800 | [diff] [blame] | 91 | int AddFirmwareKeySignature(FirmwareImage* image, const char* root_key_file); |
Gaurav Shah | 431b988 | 2010-02-12 15:54:37 -0800 | [diff] [blame] | 92 | |
| 93 | /* Add firmware and preamble signature to a firmware image [image] |
| 94 | * using the private signing key in file [signing_key_file]. |
| 95 | * |
| 96 | * Return 1 on success, 0 on failure. |
| 97 | */ |
Gaurav Shah | f5564fa | 2010-03-02 15:40:01 -0800 | [diff] [blame] | 98 | int AddFirmwareSignature(FirmwareImage* image, const char* signing_key_file); |
Gaurav Shah | 431b988 | 2010-02-12 15:54:37 -0800 | [diff] [blame] | 99 | |
Gaurav Shah | 431b988 | 2010-02-12 15:54:37 -0800 | [diff] [blame] | 100 | #endif /* VBOOT_REFERENCE_FIRMWARE_IMAGE_H_ */ |