vboot_region_tests.c 6.0 KB

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