Home | History | Annotate | Download | only in tests
      1 /* Copyright (c) 2011 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  * Tests for vboot region API
      6  */
      7 
      8 #include <stdint.h>
      9 #include <stdio.h>
     10 #include <stdlib.h>
     11 #include <string.h>
     12 
     13 #include "bmpblk_font.h"
     14 #include "gbb_header.h"
     15 #include "host_common.h"
     16 #include "rollback_index.h"
     17 #include "test_common.h"
     18 #include "vboot_common.h"
     19 #include "vboot_nvstorage.h"
     20 #include "vboot_struct.h"
     21 
     22 /* Mock data */
     23 static VbCommonParams cparams;
     24 static VbNvContext vnc;
     25 static VbSelectFirmwareParams fparams;
     26 VbSelectAndLoadKernelParams kparams;
     27 static char gbb_data[4096 + sizeof(GoogleBinaryBlockHeader)];
     28 static uint8_t shared_data[VB_SHARED_DATA_MIN_SIZE];
     29 static VbSharedDataHeader* shared = (VbSharedDataHeader*)shared_data;
     30 /* Mock TPM versions */
     31 static uint32_t mock_tpm_version;
     32 static uint32_t mock_lf_tpm_version;  /* TPM version set by LoadFirmware() */
     33 static uint32_t mock_seen_region;
     34 /* Mock return values, so we can simulate errors */
     35 static VbError_t mock_lf_retval;
     36 
     37 #define COMPRESSED_SIZE		200
     38 #define ORIGINAL_SIZE		400
     39 
     40 /* Reset mock data (for use before each test) */
     41 static void ResetMocks(void) {
     42 	GoogleBinaryBlockHeader *gbb;
     43 	BmpBlockHeader *bhdr;
     44 	ImageInfo *image_info;
     45 	ScreenLayout *layout;
     46 	int gbb_used;
     47 
     48 	Memset(&vnc, 0, sizeof(vnc));
     49 	VbNvSetup(&vnc);
     50 	VbNvTeardown(&vnc);                   /* So CRC gets generated */
     51 
     52 	Memset(&cparams, 0, sizeof(cparams));
     53 	cparams.shared_data_size = sizeof(shared_data);
     54 	cparams.shared_data_blob = shared_data;
     55 
     56 	Memset(&fparams, 0, sizeof(fparams));
     57 
     58 	Memset(gbb_data, 0, sizeof(gbb_data));
     59 	gbb = (GoogleBinaryBlockHeader *)gbb_data;
     60 	gbb->major_version = GBB_MAJOR_VER;
     61 	gbb->minor_version = GBB_MINOR_VER;
     62 	gbb->flags = 0;
     63 	gbb_used = sizeof(GoogleBinaryBlockHeader);
     64 
     65 	gbb->hwid_offset = gbb_used;
     66 	strcpy(gbb_data + gbb->hwid_offset, "Test HWID");
     67 	gbb->hwid_size = strlen(gbb_data + gbb->hwid_offset) + 1;
     68 	gbb_used = (gbb_used + gbb->hwid_size + 7) & ~7;
     69 
     70 	gbb->bmpfv_offset = gbb_used;
     71 	bhdr = (BmpBlockHeader *)(gbb_data + gbb->bmpfv_offset);
     72 	gbb->bmpfv_size = sizeof(BmpBlockHeader);
     73 	gbb_used = (gbb_used + gbb->bmpfv_size + 7) & ~7;
     74 	memcpy(bhdr->signature, BMPBLOCK_SIGNATURE, BMPBLOCK_SIGNATURE_SIZE);
     75 	bhdr->major_version = BMPBLOCK_MAJOR_VERSION;
     76 	bhdr->minor_version = BMPBLOCK_MINOR_VERSION;
     77 	bhdr->number_of_localizations = 3;
     78 	bhdr->number_of_screenlayouts = 1;
     79 
     80 	layout = (ScreenLayout *)(gbb_data + gbb_used);
     81 	gbb_used += sizeof(*layout);
     82 	layout->images[0].x = 1;
     83 	layout->images[0].image_info_offset = gbb_used - gbb->bmpfv_offset;
     84 
     85 	/* First image is uncompressed */
     86 	image_info = (ImageInfo *)(gbb_data + gbb_used);
     87 	image_info->format = FORMAT_BMP;
     88 	image_info->compressed_size = ORIGINAL_SIZE;
     89 	image_info->original_size = ORIGINAL_SIZE;
     90 	image_info->compression = COMPRESS_NONE;
     91 	gbb_used += sizeof(*image_info);
     92 	strcpy(gbb_data + gbb_used, "original");
     93 	gbb_used += ORIGINAL_SIZE;
     94 
     95 	/* Second image is compressed */
     96 	layout->images[1].image_info_offset = gbb_used - gbb->bmpfv_offset;
     97 	layout->images[1].x = 2;
     98 	image_info = (ImageInfo *)(gbb_data + gbb_used);
     99 	image_info->format = FORMAT_BMP;
    100 	image_info->compressed_size = COMPRESSED_SIZE;
    101 	image_info->original_size = ORIGINAL_SIZE;
    102 	image_info->compression = COMPRESS_LZMA1;
    103 	gbb_used += sizeof(*image_info) + COMPRESSED_SIZE;
    104 
    105 	Memset(&shared_data, 0, sizeof(shared_data));
    106 	VbSharedDataInit(shared, sizeof(shared_data));
    107 	shared->fw_keyblock_flags = 0xABCDE0;
    108 
    109 	mock_tpm_version = mock_lf_tpm_version = 0x20004;
    110 	shared->fw_version_tpm_start = mock_tpm_version;
    111 	mock_lf_retval = 0;
    112 	mock_seen_region = 0;
    113 }
    114 
    115 /****************************************************************************/
    116 /* Mocked verification functions */
    117 
    118 uint32_t SetTPMBootModeState(int developer_mode, int recovery_mode,
    119 			     uint64_t fw_keyblock_flags
    120 			     GoogleBinaryBlockHeader *gbb) {
    121   return VBERROR_SUCCESS;
    122 }
    123 
    124 VbError_t VbExNvStorageRead(uint8_t* buf) {
    125   Memcpy(buf, vnc.raw, sizeof(vnc.raw));
    126   return VBERROR_SUCCESS;
    127 }
    128 
    129 VbError_t VbExNvStorageWrite(const uint8_t* buf) {
    130   Memcpy(vnc.raw, buf, sizeof(vnc.raw));
    131   return VBERROR_SUCCESS;
    132 }
    133 
    134 VbError_t VbExRegionRead(VbCommonParams *cparams,
    135 			 enum vb_firmware_region region, uint32_t offset,
    136 			 uint32_t size, void *buf)
    137 {
    138 	if (region != VB_REGION_GBB)
    139 		return VBERROR_UNSUPPORTED_REGION;
    140 	mock_seen_region |= 1 << region;
    141 	if (offset + size > sizeof(gbb_data))
    142 		return VBERROR_REGION_READ_INVALID;
    143 	memcpy(buf, gbb_data + offset, size);
    144 	return VBERROR_SUCCESS;
    145 }
    146 
    147 VbError_t VbExDisplayImage(uint32_t x, uint32_t y,
    148                            void *buffer, uint32_t buffersize)
    149 {
    150 	switch (x) {
    151 	case 1:
    152 		TEST_STR_EQ(buffer, "original", "  uncompressed image");
    153 		break;
    154 	case 2:
    155 		TEST_STR_EQ(buffer, "decompressed", "  compressed image");
    156 		break;
    157 	default:
    158 		TEST_STR_EQ(buffer, "invalid", "  correct image");
    159 		break;
    160 	}
    161 	return VBERROR_SUCCESS;
    162 }
    163 
    164 VbError_t VbExDecompress(void *inbuf, uint32_t in_size,
    165                          uint32_t compression_type,
    166                          void *outbuf, uint32_t *out_size)
    167 {
    168 	*out_size = ORIGINAL_SIZE;
    169 	strcpy(outbuf, "decompressed");
    170 	return VBERROR_SUCCESS;
    171 }
    172 
    173 int LoadFirmware(VbCommonParams *cparams, VbSelectFirmwareParams *fparams,
    174                  VbNvContext *vnc) {
    175 	shared->fw_version_tpm = mock_lf_tpm_version;
    176 	TEST_PTR_NEQ(cparams->gbb, NULL, "  GBB allocated");
    177 	return mock_lf_retval;
    178 }
    179 
    180 /****************************************************************************/
    181 
    182 static void VbRegionReadTest(void) {
    183 	/* Should read GBB */
    184 	ResetMocks();
    185 	TEST_TRUE(1, "Normal call");
    186 	TEST_EQ(VbSelectFirmware(&cparams, &fparams), VBERROR_SUCCESS,
    187 		"  Success");
    188 	TEST_EQ(mock_seen_region, 1 << VB_REGION_GBB, "  GBB region");
    189 	TEST_PTR_EQ(cparams.gbb, NULL, "  GBB free");
    190 
    191 	ResetMocks();
    192 	TEST_EQ(VbSelectAndLoadKernel(&cparams, &kparams),
    193 		VBERROR_NO_DISK_FOUND, "Kernel");
    194 	TEST_PTR_EQ(cparams.gbb, NULL, "  GBB free");
    195 	TEST_PTR_EQ(cparams.bmp, NULL, "  BMP free");
    196 
    197 	ResetMocks();
    198 	shared->flags |= VBSD_BOOT_DEV_SWITCH_ON;
    199 	TEST_EQ(VbSelectAndLoadKernel(&cparams, &kparams),
    200 		VBERROR_NO_DISK_FOUND, "Kernel");
    201 }
    202 
    203 int main(int argc, char* argv[]) {
    204   int error_code = 0;
    205 
    206   VbRegionReadTest();
    207 
    208   if (vboot_api_stub_check_memory())
    209     error_code = 255;
    210   if (!gTestSuccess)
    211     error_code = 255;
    212 
    213   return error_code;
    214 }
    215