vboot_nvstorage_test.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* Copyright (c) 2013 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 firmware NV storage library.
  6. */
  7. #include <stdint.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "test_common.h"
  12. #include "vboot_common.h"
  13. #include "vboot_nvstorage.h"
  14. /* Single NV storage field to test */
  15. typedef struct VbNvField {
  16. VbNvParam param; /* Parameter index */
  17. uint32_t default_value; /* Expected default value */
  18. uint32_t test_value; /* Value to test writing */
  19. uint32_t test_value2; /* Second value to test writing */
  20. char* desc; /* Field description */
  21. } VbNvField;
  22. /* Array of fields to test, terminated with a field with desc==NULL. */
  23. static VbNvField nvfields[] = {
  24. {VBNV_DEBUG_RESET_MODE, 0, 1, 0, "debug reset mode"},
  25. {VBNV_TRY_B_COUNT, 0, 6, 15, "try B count"},
  26. {VBNV_RECOVERY_REQUEST, 0, 0x42, 0xED, "recovery request"},
  27. {VBNV_LOCALIZATION_INDEX, 0, 0x69, 0xB0, "localization index"},
  28. {VBNV_KERNEL_FIELD, 0, 0x12345678, 0xFEDCBA98, "kernel field"},
  29. {VBNV_DEV_BOOT_USB, 0, 1, 0, "dev boot usb"},
  30. {VBNV_DEV_BOOT_LEGACY, 0, 1, 0, "dev boot legacy"},
  31. {VBNV_DEV_BOOT_SIGNED_ONLY, 0, 1, 0, "dev boot custom"},
  32. {VBNV_DEV_BOOT_FASTBOOT_FULL_CAP, 0, 1, 0, "dev boot fastboot full cap"},
  33. {VBNV_DEV_DEFAULT_BOOT, 0, 1, 2, "dev default boot"},
  34. {VBNV_DISABLE_DEV_REQUEST, 0, 1, 0, "disable dev request"},
  35. {VBNV_CLEAR_TPM_OWNER_REQUEST, 0, 1, 0, "clear tpm owner request"},
  36. {VBNV_CLEAR_TPM_OWNER_DONE, 0, 1, 0, "clear tpm owner done"},
  37. {VBNV_TPM_REQUESTED_REBOOT, 0, 1, 0, "tpm requested reboot"},
  38. {VBNV_OPROM_NEEDED, 0, 1, 0, "oprom needed"},
  39. {VBNV_FW_TRY_COUNT, 0, 8, 15, "try count"},
  40. {VBNV_FW_TRY_NEXT, 0, 1, 0, "try next"},
  41. {VBNV_FW_TRIED, 0, 1, 0, "firmware tried"},
  42. {VBNV_FW_RESULT, VBNV_FW_RESULT_UNKNOWN, 1, 2, "firmware result"},
  43. {VBNV_FW_PREV_TRIED, 0, 1, 0, "firmware prev tried"},
  44. {VBNV_FW_PREV_RESULT, VBNV_FW_RESULT_UNKNOWN, 1, 3, "firmware prev result"},
  45. {VBNV_FASTBOOT_UNLOCK_IN_FW, 0, 1, 0, "fastboot unlock in firmware"},
  46. {VBNV_BOOT_ON_AC_DETECT, 0, 1, 0, "boot on ac detect"},
  47. {VBNV_TRY_RO_SYNC, 0, 1, 0, "try read only software sync"},
  48. {0, 0, 0, 0, NULL}
  49. };
  50. static void VbNvStorageTest(void) {
  51. VbNvField* vnf;
  52. VbNvContext c;
  53. uint8_t goodcrc;
  54. uint32_t data;
  55. memset(&c, 0xA6, sizeof(c));
  56. /* Open with invalid data should set defaults */
  57. TEST_EQ(VbNvSetup(&c), 0, "VbNvSetup()");
  58. TEST_EQ(c.raw[0], 0x70, "VbNvSetup() reset header byte");
  59. /* Close then regenerates the CRC */
  60. TEST_EQ(VbNvTeardown(&c), 0, "VbNvTeardown()");
  61. TEST_NEQ(c.raw[15], 0, "VbNvTeardown() CRC");
  62. TEST_EQ(c.raw_changed, 1, "VbNvTeardown() changed");
  63. goodcrc = c.raw[15];
  64. /* Another open-close pair should not cause further changes */
  65. VbNvSetup(&c);
  66. VbNvTeardown(&c);
  67. TEST_EQ(c.raw_changed, 0, "VbNvTeardown() didn't change");
  68. TEST_EQ(c.raw[15], goodcrc, "VbNvTeardown() CRC same");
  69. /* Perturbing the header should force defaults */
  70. c.raw[0] ^= 0x40;
  71. VbNvSetup(&c);
  72. TEST_EQ(c.raw[0], 0x70, "VbNvSetup() reset header byte again");
  73. /* Close then regenerates the CRC */
  74. VbNvTeardown(&c);
  75. TEST_EQ(c.raw_changed, 1, "VbNvTeardown() changed again");
  76. TEST_EQ(c.raw[15], goodcrc, "VbNvTeardown() CRC same again");
  77. /* So should perturbing some other byte */
  78. TEST_EQ(c.raw[11], 0, "Kernel byte starts at 0");
  79. c.raw[11] = 12;
  80. VbNvSetup(&c);
  81. TEST_EQ(c.raw[11], 0, "VbNvSetup() reset kernel byte");
  82. /* Close then regenerates the CRC */
  83. VbNvTeardown(&c);
  84. TEST_EQ(c.raw_changed, 1, "VbNvTeardown() changed again");
  85. TEST_EQ(c.raw[15], goodcrc, "VbNvTeardown() CRC same again");
  86. /* Clear the kernel and firmware flags */
  87. VbNvSetup(&c);
  88. TEST_EQ(VbNvGet(&c, VBNV_FIRMWARE_SETTINGS_RESET, &data), 0,
  89. "Get firmware settings reset");
  90. TEST_EQ(data, 1, "Firmware settings are reset");
  91. TEST_EQ(VbNvSet(&c, VBNV_FIRMWARE_SETTINGS_RESET, 0), 0,
  92. "Clear firmware settings reset");
  93. VbNvGet(&c, VBNV_FIRMWARE_SETTINGS_RESET, &data);
  94. TEST_EQ(data, 0, "Firmware settings are clear");
  95. TEST_EQ(VbNvGet(&c, VBNV_KERNEL_SETTINGS_RESET, &data), 0,
  96. "Get kernel settings reset");
  97. TEST_EQ(data, 1, "Kernel settings are reset");
  98. TEST_EQ(VbNvSet(&c, VBNV_KERNEL_SETTINGS_RESET, 0), 0,
  99. "Clear kernel settings reset");
  100. VbNvGet(&c, VBNV_KERNEL_SETTINGS_RESET, &data);
  101. TEST_EQ(data, 0, "Kernel settings are clear");
  102. TEST_EQ(c.raw[0], 0x40, "Header byte now just has the header bit");
  103. VbNvTeardown(&c);
  104. /* That should have changed the CRC */
  105. TEST_NEQ(c.raw[15], goodcrc, "VbNvTeardown() CRC changed due to flags clear");
  106. /* Test explicitly setting the reset flags again */
  107. VbNvSetup(&c);
  108. VbNvSet(&c, VBNV_FIRMWARE_SETTINGS_RESET, 1);
  109. VbNvGet(&c, VBNV_FIRMWARE_SETTINGS_RESET, &data);
  110. TEST_EQ(data, 1, "Firmware settings forced reset");
  111. VbNvSet(&c, VBNV_FIRMWARE_SETTINGS_RESET, 0);
  112. VbNvSet(&c, VBNV_KERNEL_SETTINGS_RESET, 1);
  113. VbNvGet(&c, VBNV_KERNEL_SETTINGS_RESET, &data);
  114. TEST_EQ(data, 1, "Kernel settings forced reset");
  115. VbNvSet(&c, VBNV_KERNEL_SETTINGS_RESET, 0);
  116. VbNvTeardown(&c);
  117. /* Get/set an invalid field */
  118. VbNvSetup(&c);
  119. TEST_EQ(VbNvGet(&c, -1, &data), 1, "Get invalid setting");
  120. TEST_EQ(VbNvSet(&c, -1, 0), 1, "Set invalid setting");
  121. VbNvTeardown(&c);
  122. /* Test other fields */
  123. VbNvSetup(&c);
  124. /* Test all defaults first, since some fields alias onto others */
  125. for (vnf = nvfields; vnf->desc; vnf++) {
  126. printf("Testing field: %s\n", vnf->desc);
  127. TEST_EQ(VbNvGet(&c, vnf->param, &data), 0, " get");
  128. TEST_EQ(data, vnf->default_value, " default");
  129. }
  130. /* Now test get/set */
  131. for (vnf = nvfields; vnf->desc; vnf++) {
  132. printf("Testing field: %s\n", vnf->desc);
  133. TEST_EQ(VbNvSet(&c, vnf->param, vnf->test_value), 0, " set 1");
  134. TEST_EQ(VbNvGet(&c, vnf->param, &data), 0, " get 1");
  135. TEST_EQ(data, vnf->test_value, " value 1");
  136. TEST_EQ(VbNvSet(&c, vnf->param, vnf->test_value2), 0, " set 2");
  137. TEST_EQ(VbNvGet(&c, vnf->param, &data), 0, " get 2");
  138. TEST_EQ(data, vnf->test_value2, " value 2");
  139. }
  140. VbNvTeardown(&c);
  141. /* None of those changes should have caused a reset to defaults */
  142. VbNvSetup(&c);
  143. VbNvGet(&c, VBNV_FIRMWARE_SETTINGS_RESET, &data);
  144. TEST_EQ(data, 0, "Firmware settings are still clear");
  145. VbNvGet(&c, VBNV_KERNEL_SETTINGS_RESET, &data);
  146. TEST_EQ(data, 0, "Kernel settings are still clear");
  147. VbNvTeardown(&c);
  148. /* Verify writing identical settings doesn't cause the CRC to regenerate */
  149. VbNvSetup(&c);
  150. TEST_EQ(c.regenerate_crc, 0, "No regen CRC on open");
  151. for (vnf = nvfields; vnf->desc; vnf++)
  152. TEST_EQ(VbNvSet(&c, vnf->param, vnf->test_value2), 0, vnf->desc);
  153. TEST_EQ(c.regenerate_crc, 0, "No regen CRC if data not changed");
  154. VbNvTeardown(&c);
  155. TEST_EQ(c.raw_changed, 0, "No raw change if data not changed");
  156. /* Test out-of-range fields mapping to defaults */
  157. VbNvSetup(&c);
  158. VbNvSet(&c, VBNV_TRY_B_COUNT, 16);
  159. VbNvGet(&c, VBNV_TRY_B_COUNT, &data);
  160. TEST_EQ(data, 15, "Try b count out of range");
  161. VbNvSetup(&c);
  162. VbNvSet(&c, VBNV_FW_TRY_COUNT, 16);
  163. VbNvGet(&c, VBNV_FW_TRY_COUNT, &data);
  164. TEST_EQ(data, 15, "Try count out of range");
  165. VbNvSet(&c, VBNV_RECOVERY_REQUEST, 0x101);
  166. VbNvGet(&c, VBNV_RECOVERY_REQUEST, &data);
  167. TEST_EQ(data, VBNV_RECOVERY_LEGACY, "Recovery request out of range");
  168. VbNvSet(&c, VBNV_LOCALIZATION_INDEX, 0x102);
  169. VbNvGet(&c, VBNV_LOCALIZATION_INDEX, &data);
  170. TEST_EQ(data, 0, "Localization index out of range");
  171. VbNvSet(&c, VBNV_FW_RESULT, VBNV_FW_RESULT_UNKNOWN + 100);
  172. VbNvGet(&c, VBNV_FW_RESULT, &data);
  173. TEST_EQ(data, VBNV_FW_RESULT_UNKNOWN, "Firmware result out of range");
  174. VbNvSet(&c, VBNV_DEV_DEFAULT_BOOT, VBNV_DEV_DEFAULT_BOOT_DISK + 100);
  175. VbNvGet(&c, VBNV_DEV_DEFAULT_BOOT, &data);
  176. TEST_EQ(data, VBNV_DEV_DEFAULT_BOOT_DISK, "Firmware result out of range");
  177. VbNvTeardown(&c);
  178. }
  179. int main(int argc, char* argv[])
  180. {
  181. VbNvStorageTest();
  182. return gTestSuccess ? 0 : 255;
  183. }