vboot_api.h 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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. /* APIs provided by firmware to vboot_reference.
  6. *
  7. * General notes:
  8. *
  9. * All verified boot functions now start with "Vb" for namespace clarity. This
  10. * fixes the problem where uboot and vboot both defined assert().
  11. *
  12. * Verified boot APIs to be implemented by the calling firmware and exported to
  13. * vboot_reference start with "VbEx".
  14. *
  15. * TODO: split this file into a vboot_entry_points.h file which contains the
  16. * entry points for the firmware to call vboot_reference, and a
  17. * vboot_firmware_exports.h which contains the APIs to be implemented by the
  18. * calling firmware and exported to vboot_reference.
  19. */
  20. #ifndef VBOOT_REFERENCE_VBOOT_API_H_
  21. #define VBOOT_REFERENCE_VBOOT_API_H_
  22. #include <stdint.h>
  23. #include <stdlib.h>
  24. #include "gpt.h"
  25. /*****************************************************************************/
  26. /* Error codes */
  27. /*
  28. * Functions which return an error all return this type. This is a 32-bit
  29. * value rather than an int so it's consistent across UEFI, which is 32-bit
  30. * during PEI and 64-bit during DXE/BDS.
  31. */
  32. typedef uint32_t VbError_t;
  33. /*
  34. * Define test_mockable for mocking functions.
  35. */
  36. #define test_mockable __attribute__((weak))
  37. /*
  38. * Predefined error numbers. Success is 0. Errors are non-zero, but differ
  39. * between functions. For example, the TPM functions may pass through TPM
  40. * error codes, some of which may be recoverable.
  41. */
  42. enum VbErrorPredefined_t {
  43. /* No error; function completed successfully. */
  44. VBERROR_SUCCESS = 0,
  45. /*
  46. * The verified boot entry points VbInit(), VbSelectFirmware(),
  47. * VbSelectAndLoadKernel() may return the following errors.
  48. */
  49. /* Unknown error */
  50. VBERROR_UNKNOWN = 0x10000,
  51. /* Unable to initialize shared data */
  52. VBERROR_INIT_SHARED_DATA = 0x10001,
  53. /* Error resuming TPM during a S3 resume */
  54. VBERROR_TPM_S3_RESUME = 0x10002,
  55. /* VbSelectFirmware() failed to find a valid firmware */
  56. VBERROR_LOAD_FIRMWARE = 0x10003,
  57. /* Unable to write firmware versions to TPM */
  58. VBERROR_TPM_WRITE_FIRMWARE = 0x10004,
  59. /* Unable to lock firmware versions in TPM */
  60. VBERROR_TPM_LOCK_FIRMWARE = 0x10005,
  61. /* Unable to set boot mode state in TPM */
  62. VBERROR_TPM_SET_BOOT_MODE_STATE = 0x10006,
  63. /* Calling firmware needs to perform a reboot. */
  64. VBERROR_REBOOT_REQUIRED = 0x10007,
  65. /* Unable to set up TPM */
  66. VBERROR_TPM_FIRMWARE_SETUP = 0x10008,
  67. /* Unable to read kernel versions from TPM */
  68. VBERROR_TPM_READ_KERNEL = 0x10009,
  69. /* Attempt to load developer-only firmware with developer switch off */
  70. VBERROR_DEV_FIRMWARE_SWITCH_MISMATCH = 0x1000A,
  71. /* Unable to write kernel versions to TPM */
  72. VBERROR_TPM_WRITE_KERNEL = 0x1000B,
  73. /* Unable to lock kernel versions in TPM */
  74. VBERROR_TPM_LOCK_KERNEL = 0x1000C,
  75. /* Calling firmware requested shutdown via VbExIsShutdownRequested() */
  76. VBERROR_SHUTDOWN_REQUESTED = 0x1000D,
  77. /* Unable to find a boot device on which to look for a kernel */
  78. VBERROR_NO_DISK_FOUND = 0x1000E,
  79. /* No OS kernel found on any boot device */
  80. VBERROR_NO_KERNEL_FOUND = 0x1000F,
  81. /* All OS kernels found were invalid (corrupt, improperly signed...) */
  82. VBERROR_INVALID_KERNEL_FOUND = 0x10010,
  83. /* VbSelectAndLoadKernel() requested recovery mode */
  84. VBERROR_LOAD_KERNEL_RECOVERY = 0x10011,
  85. /* Other error inside VbSelectAndLoadKernel() */
  86. VBERROR_LOAD_KERNEL = 0x10012,
  87. /* Invalid Google binary block */
  88. VBERROR_INVALID_GBB = 0x10013,
  89. /* Invalid bitmap volume */
  90. VBERROR_INVALID_BMPFV = 0x10014,
  91. /* Invalid screen index */
  92. VBERROR_INVALID_SCREEN_INDEX = 0x10015,
  93. /* Simulated (test) error */
  94. VBERROR_SIMULATED = 0x10016,
  95. /* Invalid parameter */
  96. VBERROR_INVALID_PARAMETER = 0x10017,
  97. /* VbExBeep() can't make sounds at all */
  98. VBERROR_NO_SOUND = 0x10018,
  99. /* VbExBeep() can't make sound in the background */
  100. VBERROR_NO_BACKGROUND_SOUND = 0x10019,
  101. /* Developer has requested a BIOS shell */
  102. VBERROR_BIOS_SHELL_REQUESTED = 0x10020,
  103. /* Need VGA and don't have it, or vice-versa */
  104. VBERROR_VGA_OPROM_MISMATCH = 0x10021,
  105. /* Need EC to reboot to read-only code */
  106. VBERROR_EC_REBOOT_TO_RO_REQUIRED = 0x10022,
  107. /* Invalid region read parameters */
  108. VBERROR_REGION_READ_INVALID = 0x10023,
  109. /* Cannot read from region */
  110. VBERROR_REGION_READ_FAILED = 0x10024,
  111. /* Unsupported region type */
  112. VBERROR_UNSUPPORTED_REGION = 0x10025,
  113. /* No image present (returned from VbGbbReadImage() for missing image) */
  114. VBERROR_NO_IMAGE_PRESENT = 0x10026,
  115. /* failed to draw screen */
  116. VBERROR_SCREEN_DRAW = 0x10027,
  117. /* failed to jump to RW image */
  118. VBERROR_RW_JUMP_FAILED = 0x10028,
  119. /* Error reading FWMP from TPM (note: not present is not an error) */
  120. VBERROR_TPM_READ_FWMP = 0x10029,
  121. /* VbExEcGetExpectedRWHash() may return the following codes */
  122. /* Compute expected RW hash from the EC image; BIOS doesn't have it */
  123. VBERROR_EC_GET_EXPECTED_HASH_FROM_IMAGE = 0x20000,
  124. };
  125. /*****************************************************************************/
  126. /* Main entry points from firmware into vboot_reference */
  127. /*
  128. * Minimum and recommended size of shared_data_blob in bytes. Shared data blob
  129. * is used to communicate data between calls to VbInit(), VbSelectFirmware(),
  130. * the OS. Minimum size is enough to hold all required data for verified boot
  131. * but may not be able to hold debug output.
  132. */
  133. #define VB_SHARED_DATA_MIN_SIZE 3072
  134. #define VB_SHARED_DATA_REC_SIZE 16384
  135. /*
  136. * Data passed by firmware to VbInit(), VbSelectFirmware() and
  137. * VbSelectAndLoadKernel().
  138. *
  139. * Note that in UEFI, these are called by different phases in different
  140. * processor modes (VbInit() and VbSelectFirmware() = 32-bit PEI,
  141. * VbSelectAndLoadKernel() = 64-bit BDS), so the data may be at a different
  142. * location between calls.
  143. */
  144. typedef struct VbCommonParams {
  145. /* Pointer to GBB data */
  146. void *gbb_data;
  147. /* Size of GBB data in bytes */
  148. uint32_t gbb_size;
  149. /*
  150. * Shared data blob for data shared between verified boot entry points.
  151. * This should be at least VB_SHARED_DATA_MIN_SIZE bytes long, and
  152. * ideally is VB_SHARED_DATA_REC_SIZE bytes long.
  153. */
  154. /* Pointer to shared data blob buffer */
  155. void *shared_data_blob;
  156. /*
  157. * On input, set to size of shared data blob buffer, in bytes. On
  158. * output, this will contain the actual data size placed into the
  159. * buffer.
  160. */
  161. uint32_t shared_data_size;
  162. /*
  163. * Internal context/data for verified boot, to maintain state during
  164. * calls to other API functions such as VbExHashFirmwareBody().
  165. * Allocated and freed inside the entry point; firmware should not look
  166. * at this.
  167. */
  168. void *vboot_context;
  169. /*
  170. * Internal context/data for firmware / VbExHashFirmwareBody(). Needed
  171. * because the PEI phase of UEFI boot runs out of ROM and thus can't
  172. * modify global variables; everything needs to get passed around on
  173. * the stack.
  174. */
  175. void *caller_context;
  176. /* For internal use of Vboot - do not examine or modify! */
  177. struct GoogleBinaryBlockHeader *gbb;
  178. struct BmpBlockHeader *bmp;
  179. } VbCommonParams;
  180. /* Flags for VbInitParams.flags */
  181. /* Developer switch was on at boot time. */
  182. #define VB_INIT_FLAG_DEV_SWITCH_ON 0x00000001
  183. /* Recovery button was pressed at boot time. */
  184. #define VB_INIT_FLAG_REC_BUTTON_PRESSED 0x00000002
  185. /* Hardware write protect was enabled at boot time. */
  186. #define VB_INIT_FLAG_WP_ENABLED 0x00000004
  187. /* This is a S3 resume, not a normal boot. */
  188. #define VB_INIT_FLAG_S3_RESUME 0x00000008
  189. /*
  190. * Previous boot attempt failed for reasons external to verified boot (RAM
  191. * init failure, SSD missing, etc.).
  192. *
  193. * TODO: add a field to VbInitParams which holds a reason code, and report
  194. * that via VbSharedData.
  195. */
  196. #define VB_INIT_FLAG_PREVIOUS_BOOT_FAIL 0x00000010
  197. /*
  198. * Calling firmware supports read only firmware for normal/developer boot path.
  199. */
  200. #define VB_INIT_FLAG_RO_NORMAL_SUPPORT 0x00000020
  201. /*
  202. * This platform does not have a physical dev-switch, so we must rely on a
  203. * virtual switch (kept in the TPM) instead. When this flag is set,
  204. * VB_INIT_FLAG_DEV_SWITCH_ON is ignored.
  205. */
  206. #define VB_INIT_FLAG_VIRTUAL_DEV_SWITCH 0x00000040
  207. /* Set when the VGA Option ROM has been loaded already. */
  208. #define VB_INIT_FLAG_OPROM_LOADED 0x00000080
  209. /* Set if we care about the VGA Option ROM - some platforms don't. */
  210. #define VB_INIT_FLAG_OPROM_MATTERS 0x00000100
  211. /* EC on this platform supports EC software sync. */
  212. #define VB_INIT_FLAG_EC_SOFTWARE_SYNC 0x00000200
  213. /* EC on this platform is slow to update. */
  214. #define VB_INIT_FLAG_EC_SLOW_UPDATE 0x00000400
  215. /*
  216. * Software write protect was enabled at boot time. This is separate from the
  217. * HW write protect. Both must be set for flash write protection to work.
  218. */
  219. #define VB_INIT_FLAG_SW_WP_ENABLED 0x00000800
  220. /*
  221. * This platform does not have a physical recovery switch which, when present,
  222. * can (and should) be used for additional physical presence checks.
  223. */
  224. #define VB_INIT_FLAG_VIRTUAL_REC_SWITCH 0x00001000
  225. /* Set when we are calling VbInit() before loading Option ROMs */
  226. #define VB_INIT_FLAG_BEFORE_OPROM_LOAD 0x00002000
  227. /* Allow USB boot on transition to dev */
  228. #define VB_INIT_FLAG_ALLOW_USB_BOOT 0x00004000
  229. /* Set when we can't reliably identify boot failures. This prevents
  230. * the boot-try counters from decrementing.
  231. */
  232. #define VB_INIT_FLAG_NOFAIL_BOOT 0x00008000
  233. /*
  234. * Output flags for VbInitParams.out_flags. Used to indicate potential boot
  235. * paths and configuration to the calling firmware early in the boot process,
  236. * so that it can properly configure itself for the capabilities subsequently
  237. * required by VbSelectFirmware() and VbSelectAndLoadKernel().
  238. */
  239. /*
  240. * Enable recovery path. Do not rely on any rewritable data (cached RAM
  241. * timings, etc.). Reliable operation is more important than boot speed.
  242. */
  243. #define VB_INIT_OUT_ENABLE_RECOVERY 0x00000001
  244. /* RAM must be cleared before calling VbSelectFirmware(). */
  245. #define VB_INIT_OUT_CLEAR_RAM 0x00000002
  246. /*
  247. * Load display drivers; VbExDisplay*() functions may be called. If this flag
  248. * is not present, VbExDisplay*() functions will not be called this boot.
  249. */
  250. #define VB_INIT_OUT_ENABLE_DISPLAY 0x00000004
  251. /*
  252. * Load USB storage drivers; VbExDisk*() functions may be called with the
  253. * VB_DISK_FLAG_REMOVABLE flag. If this flag is not present, VbExDisk*()
  254. * functions will only be called for fixed disks.
  255. */
  256. #define VB_INIT_OUT_ENABLE_USB_STORAGE 0x00000008
  257. /* If this is a S3 resume, do a debug reset boot instead */
  258. #define VB_INIT_OUT_S3_DEBUG_BOOT 0x00000010
  259. /* BIOS should load any PCI option ROMs it finds, not just internal video */
  260. #define VB_INIT_OUT_ENABLE_OPROM 0x00000020
  261. /* BIOS may be asked to boot something other than ChromeOS */
  262. #define VB_INIT_OUT_ENABLE_ALTERNATE_OS 0x00000040
  263. /* Enable developer path. */
  264. #define VB_INIT_OUT_ENABLE_DEVELOPER 0x00000080
  265. /* Data only used by VbInit() */
  266. typedef struct VbInitParams {
  267. /* Inputs to VbInit() */
  268. /* Flags (see VB_INIT_FLAG_*) */
  269. uint32_t flags;
  270. /* Outputs from VbInit(); valid only if it returns success. */
  271. /* Output flags for firmware; see VB_INIT_OUT_*) */
  272. uint32_t out_flags;
  273. } VbInitParams;
  274. /*
  275. * Firmware types for VbHashFirmwareBody() and
  276. * VbSelectFirmwareParams.selected_firmware. Note that we store these in a
  277. * uint32_t because enum maps to int, which isn't fixed-size.
  278. */
  279. enum VbSelectFirmware_t {
  280. /* Recovery mode */
  281. VB_SELECT_FIRMWARE_RECOVERY = 0,
  282. /* Rewritable firmware A/B for normal or developer path */
  283. VB_SELECT_FIRMWARE_A = 1,
  284. VB_SELECT_FIRMWARE_B = 2,
  285. /* Read only firmware for normal or developer path. */
  286. VB_SELECT_FIRMWARE_READONLY = 3,
  287. VB_SELECT_FIRMWARE_COUNT,
  288. };
  289. /* Data only used by VbSelectFirmware() */
  290. typedef struct VbSelectFirmwareParams {
  291. /* Inputs to VbSelectFirmware() */
  292. /* Key block + preamble for firmware A */
  293. void *verification_block_A;
  294. /* Key block + preamble for firmware B */
  295. void *verification_block_B;
  296. /* Verification block A size in bytes */
  297. uint32_t verification_size_A;
  298. /* Verification block B size in bytes */
  299. uint32_t verification_size_B;
  300. /* Outputs from VbSelectFirmware(); valid only if it returns success. */
  301. /* Main firmware to run; see VB_SELECT_FIRMWARE_*. */
  302. uint32_t selected_firmware;
  303. } VbSelectFirmwareParams;
  304. /*
  305. * We use disk handles rather than indices. Using indices causes problems if
  306. * a disk is removed/inserted in the middle of processing.
  307. */
  308. typedef void *VbExDiskHandle_t;
  309. /* Data used only by VbSelectAndLoadKernel() */
  310. typedef struct VbSelectAndLoadKernelParams {
  311. /* Inputs to VbSelectAndLoadKernel() */
  312. /* Destination buffer for kernel (normally at 0x100000 on x86) */
  313. void *kernel_buffer;
  314. /* Size of kernel buffer in bytes */
  315. uint32_t kernel_buffer_size;
  316. /* input flags. Currently used for detachables */
  317. uint32_t inflags;
  318. /*
  319. * Outputs from VbSelectAndLoadKernel(); valid only if it returns
  320. * success.
  321. */
  322. /* Handle of disk containing loaded kernel */
  323. VbExDiskHandle_t disk_handle;
  324. /* Partition number on disk to boot (1...M) */
  325. uint32_t partition_number;
  326. /* Address of bootloader image in RAM */
  327. uint64_t bootloader_address;
  328. /* Size of bootloader image in bytes */
  329. uint32_t bootloader_size;
  330. /* UniquePartitionGuid for boot partition */
  331. uint8_t partition_guid[16];
  332. /* Flags set by signer */
  333. uint32_t flags;
  334. /*
  335. * TODO: in H2C, all that pretty much just gets passed to the
  336. * bootloader as KernelBootloaderOptions, though the disk handle is
  337. * passed as an index instead of a handle. Is that used anymore now
  338. * that we're passing partition_guid?
  339. */
  340. } VbSelectAndLoadKernelParams;
  341. /* Flag to indicate using detachable menu ui (arrow up/down+power)
  342. * instead of traditional FW screens with ctrl+D, ctrl+U, etc.
  343. */
  344. #define VB_SALK_INFLAGS_ENABLE_DETACHABLE_UI (1 << 0)
  345. /**
  346. * Select and loads the kernel.
  347. *
  348. * Returns VBERROR_SUCCESS if success, non-zero if error; on error, caller
  349. * should reboot. */
  350. VbError_t VbSelectAndLoadKernel(VbCommonParams *cparams,
  351. VbSelectAndLoadKernelParams *kparams);
  352. /**
  353. * Verify Kernel Image loaded in memory.
  354. *
  355. * This routine is used by fastboot boot command to verify the kernel image in
  356. * memory sent by the host device using fastboot protocol. It checks if the
  357. * image in memory is signed using official recovery keys. In case of GBB
  358. * override to allow full fastboot functionality, it checks image integrity, but
  359. * does not check the image signature.
  360. *
  361. * @param cparams Common parameters, e.g. use member caller_context
  362. * to point to useful context data
  363. * @param kparams kernel params
  364. * @param boot_image Image in memory that needs to be verified
  365. * @param image_size Size of the image in memory
  366. * @return VBERROR_... error, VBERROR_SUCCESS on success.
  367. */
  368. VbError_t VbVerifyMemoryBootImage(VbCommonParams *cparams,
  369. VbSelectAndLoadKernelParams *kparams,
  370. void *boot_image,
  371. size_t image_size);
  372. /**
  373. * Fastboot API to enter dev mode.
  374. *
  375. * This routine is used by fastboot oem unlock command to switch the device into
  376. * dev mode.
  377. *
  378. * NOTE: The caller MUST be in read-only firmware, and MUST have just obtained
  379. * explicit physical confirmation from the user via a trusted input method
  380. * before calling this function! Also, on successful return from this function,
  381. * the caller needs to reboot the device immediately for changes to take effect.
  382. *
  383. * @return VBERROR_... error, VBERROR_SUCCESS on success.
  384. */
  385. VbError_t VbUnlockDevice(void);
  386. /**
  387. * Fastboot API to enter normal mode.
  388. *
  389. * This routine is used by fastboot oem lock command to switch the device into
  390. * normal mode.
  391. *
  392. * NOTE: On successful return from this function, the caller needs to reboot the
  393. * device immediately for changes to take effect. This routine just stores a
  394. * request, which will be handled by RO firmware on next reboot.
  395. *
  396. * @return VBERROR_... error, VBERROR_SUCCESS on success.
  397. */
  398. VbError_t VbLockDevice(void);
  399. /*****************************************************************************/
  400. /* Debug output (from utility.h) */
  401. /**
  402. * Output an error message and quit. Does not return. Supports
  403. * printf()-style formatting.
  404. */
  405. void VbExError(const char *format, ...);
  406. /**
  407. * Output a debug message. Supports printf()-style formatting.
  408. */
  409. void VbExDebug(const char *format, ...)
  410. __attribute__ ((format (__printf__, 1, 2)));
  411. /*****************************************************************************/
  412. /* Timer and delay (first two from utility.h) */
  413. /**
  414. * Read a high-resolution timer. Returns the current timer value in arbitrary
  415. * units.
  416. *
  417. * This is intended for benchmarking, so this call MUST be fast. The timer
  418. * frequency must be >1 KHz (preferably >1 MHz), and the timer must not wrap
  419. * around for at least 10 minutes. It is preferable (but not required) that
  420. * the timer be initialized to 0 at boot.
  421. *
  422. * It is assumed that the firmware has some other way of communicating the
  423. * timer frequency to the OS. For example, on x86 we use TSC, and the OS
  424. * kernel reports the initial TSC value at kernel-start and calculates the
  425. * frequency. */
  426. uint64_t VbExGetTimer(void);
  427. /**
  428. * Delay for at least the specified number of milliseconds. Should be accurate
  429. * to within 10% (a requested delay of 1000 ms should result in an actual delay
  430. * of between 1000 - 1100 ms).
  431. */
  432. void VbExSleepMs(uint32_t msec);
  433. /**
  434. * Play a beep tone of the specified frequency in Hz and duration in msec.
  435. * This is effectively a VbSleep() variant that makes noise.
  436. *
  437. * If the audio codec can run in the background, then:
  438. * zero frequency means OFF, non-zero frequency means ON
  439. * zero msec means return immediately, non-zero msec means delay (and
  440. * then OFF if needed)
  441. * otherwise,
  442. * non-zero msec and non-zero frequency means ON, delay, OFF, return
  443. * zero msec or zero frequency means do nothing and return immediately
  444. *
  445. * The return value is used by the caller to determine the capabilities. The
  446. * implementation should always do the best it can if it cannot fully support
  447. * all features - for example, beeping at a fixed frequency if frequency
  448. * support is not available. At a minimum, it must delay for the specified
  449. * non-zero duration.
  450. */
  451. VbError_t VbExBeep(uint32_t msec, uint32_t frequency);
  452. /*****************************************************************************/
  453. /* TPM (from tlcl_stub.h) */
  454. /**
  455. * Initialize the stub library. */
  456. VbError_t VbExTpmInit(void);
  457. /**
  458. * Close and open the device. This is needed for running more complex commands
  459. * at user level, such as TPM_TakeOwnership, since the TPM device can be opened
  460. * only by one process at a time.
  461. */
  462. VbError_t VbExTpmClose(void);
  463. VbError_t VbExTpmOpen(void);
  464. /**
  465. * Send a request_length-byte request to the TPM and receive a response. On
  466. * input, response_length is the size of the response buffer in bytes. On
  467. * exit, response_length is set to the actual received response length in
  468. * bytes. */
  469. VbError_t VbExTpmSendReceive(const uint8_t *request, uint32_t request_length,
  470. uint8_t *response, uint32_t *response_length);
  471. /*****************************************************************************/
  472. /* Non-volatile storage */
  473. #define VBNV_BLOCK_SIZE 16 /* Size of NV storage block in bytes */
  474. /**
  475. * Read the VBNV_BLOCK_SIZE-byte non-volatile storage into buf.
  476. */
  477. VbError_t VbExNvStorageRead(uint8_t *buf);
  478. /**
  479. * Write the VBNV_BLOCK_SIZE-byte non-volatile storage from buf.
  480. */
  481. VbError_t VbExNvStorageWrite(const uint8_t *buf);
  482. /*****************************************************************************/
  483. /* Firmware / EEPROM access (previously in load_firmware_fw.h) */
  484. /**
  485. * Calculate the hash of the firmware body data for [firmware_index], which is
  486. * either VB_SELECT_FIRMWARE_A or VB_SELECT_FIRMWARE B.
  487. *
  488. * This function must call VbUpdateFirmwareBodyHash() before returning, to
  489. * update the secure hash for the firmware image. For best performance, the
  490. * implementation should call VbUpdateFirmwareBodyHash() periodically during
  491. * the read, so that updating the hash can be pipelined with the read. If the
  492. * reader cannot update the hash during the read process, it should call
  493. * VbUpdateFirmwareBodyHash() on the entire firmware data after the read,
  494. * before returning.
  495. *
  496. * It is recommended that the firmware use this call to copy the requested
  497. * firmware body from EEPROM into RAM, so that it doesn't need to do a second
  498. * slow copy from EEPROM to RAM if this firmware body is selected.
  499. *
  500. * Note this function doesn't actually pass the firmware body data to verified
  501. * boot, because verified boot doesn't actually need the firmware body, just
  502. * its hash. This is important on x86, where the firmware is stored
  503. * compressed. We hash the compressed data, but the BIOS decompresses it
  504. * during read. Simply updating a hash is compatible with the x86
  505. * read-and-decompress pipeline.
  506. */
  507. VbError_t VbExHashFirmwareBody(VbCommonParams *cparams,
  508. uint32_t firmware_index);
  509. /*****************************************************************************/
  510. /* Disk access (previously in boot_device.h) */
  511. /* Flags for VbDisk APIs */
  512. /* Disk is removable. Example removable disks: SD cards, USB keys. */
  513. #define VB_DISK_FLAG_REMOVABLE 0x00000001
  514. /*
  515. * Disk is fixed. If this flag is present, disk is internal to the system and
  516. * not removable. Example fixed disks: internal SATA SSD, eMMC.
  517. */
  518. #define VB_DISK_FLAG_FIXED 0x00000002
  519. /*
  520. * Note that VB_DISK_FLAG_REMOVABLE and VB_DISK_FLAG_FIXED are
  521. * mutually-exclusive for a single disk. VbExDiskGetInfo() may specify both
  522. * flags to request disks of both types in a single call.
  523. *
  524. * At some point we could specify additional flags, but we don't currently
  525. * have a way to make use of these:
  526. *
  527. * USB Device is known to be attached to USB. Note that the SD
  528. * card reader inside x86 systems is attached to USB so this
  529. * isn't super useful.
  530. * SD Device is known to be a SD card. Note that external card
  531. * readers might not return this information, so also of
  532. * questionable use.
  533. * READ_ONLY Device is known to be read-only. Could be used by recovery
  534. * when processing read-only recovery image.
  535. */
  536. /*
  537. * Disks are used in two ways:
  538. * - As a random-access device to read and write the GPT
  539. * - As a streaming device to read the kernel
  540. * These are implemented differently on raw NAND vs eMMC/SATA/USB
  541. * - On eMMC/SATA/USB, both of these refer to the same underlying
  542. * storage, so they have the same size and LBA size. In this case,
  543. * the GPT should not point to the same address as itself.
  544. * - On raw NAND, the GPT is held on a portion of the SPI flash.
  545. * Random access GPT operations refer to the SPI and streaming
  546. * operations refer to NAND. The GPT may therefore point into
  547. * the same offsets as itself.
  548. * These types are distinguished by the following flag and VbDiskInfo
  549. * has separate fields to describe the random-access ("GPT") and
  550. * streaming aspects of the disk. If a disk is random-access (i.e.
  551. * not raw NAND) then these fields are equal.
  552. */
  553. #define VB_DISK_FLAG_EXTERNAL_GPT 0x00000004
  554. /* Information on a single disk */
  555. typedef struct VbDiskInfo {
  556. /* Disk handle */
  557. VbExDiskHandle_t handle;
  558. /* Size of a random-access LBA sector in bytes */
  559. uint64_t bytes_per_lba;
  560. /* Number of random-access LBA sectors on the device.
  561. * If streaming_lba_count is 0, this stands in for the size of the
  562. * randomly accessed portion as well as the streaming portion.
  563. * Otherwise, this is only the randomly-accessed portion. */
  564. uint64_t lba_count;
  565. /* Number of streaming sectors on the device */
  566. uint64_t streaming_lba_count;
  567. /* Flags (see VB_DISK_FLAG_* constants) */
  568. uint32_t flags;
  569. /*
  570. * Optional name string, for use in debugging. May be empty or null if
  571. * not available.
  572. */
  573. const char *name;
  574. } VbDiskInfo;
  575. /**
  576. * Store information into [info] for all disks (storage devices) attached to
  577. * the system which match all of the disk_flags.
  578. *
  579. * On output, count indicates how many disks are present, and [infos_ptr]
  580. * points to a [count]-sized array of VbDiskInfo structs with the information
  581. * on those disks; this pointer must be freed by calling VbExDiskFreeInfo().
  582. * If count=0, infos_ptr may point to NULL. If [infos_ptr] points to NULL
  583. * because count=0 or error, it is not necessary to call VbExDiskFreeInfo().
  584. *
  585. * A multi-function device (such as a 4-in-1 card reader) should provide
  586. * multiple disk handles.
  587. *
  588. * The firmware must not alter or free the list pointed to by [infos_ptr] until
  589. * VbExDiskFreeInfo() is called.
  590. */
  591. VbError_t VbExDiskGetInfo(VbDiskInfo **infos_ptr, uint32_t *count,
  592. uint32_t disk_flags);
  593. /**
  594. * Free a disk information list [infos] previously returned by
  595. * VbExDiskGetInfo(). If [preserve_handle] != NULL, the firmware must ensure
  596. * that handle remains valid after this call; all other handles from the info
  597. * list need not remain valid after this call.
  598. */
  599. VbError_t VbExDiskFreeInfo(VbDiskInfo *infos,
  600. VbExDiskHandle_t preserve_handle);
  601. /**
  602. * Read lba_count LBA sectors, starting at sector lba_start, from the disk,
  603. * into the buffer.
  604. *
  605. * This is used for random access to the GPT. It is not for the partition
  606. * contents. The upper limit is lba_count.
  607. *
  608. * If the disk handle is invalid (for example, the handle refers to a disk
  609. * which as been removed), the function must return error but must not
  610. * crash.
  611. */
  612. VbError_t VbExDiskRead(VbExDiskHandle_t handle, uint64_t lba_start,
  613. uint64_t lba_count, void *buffer);
  614. /**
  615. * Write lba_count LBA sectors, starting at sector lba_start, to the disk, from
  616. * the buffer.
  617. *
  618. * This is used for random access to the GPT. It does not (necessarily) access
  619. * the streaming portion of the device.
  620. *
  621. * If the disk handle is invalid (for example, the handle refers to a disk
  622. * which as been removed), the function must return error but must not
  623. * crash.
  624. */
  625. VbError_t VbExDiskWrite(VbExDiskHandle_t handle, uint64_t lba_start,
  626. uint64_t lba_count, const void *buffer);
  627. /* Streaming read interface */
  628. typedef void *VbExStream_t;
  629. /**
  630. * Open a stream on a disk
  631. *
  632. * @param handle Disk to open the stream against
  633. * @param lba_start Starting sector offset within the disk to stream from
  634. * @param lba_count Maximum extent of the stream in sectors
  635. * @param stream out-paramter for the generated stream
  636. *
  637. * @return Error code, or VBERROR_SUCCESS.
  638. *
  639. * This is used for access to the contents of the actual partitions on the
  640. * device. It is not used to access the GPT. The size of the content addressed
  641. * is within streaming_lba_count.
  642. */
  643. VbError_t VbExStreamOpen(VbExDiskHandle_t handle, uint64_t lba_start,
  644. uint64_t lba_count, VbExStream_t *stream_ptr);
  645. /**
  646. * Read from a stream on a disk
  647. *
  648. * @param stream Stream to read from
  649. * @param bytes Number of bytes to read
  650. * @param buffer Destination to read into
  651. *
  652. * @return Error code, or VBERROR_SUCCESS. Failure to read as much data as
  653. * requested is an error.
  654. *
  655. * This is used for access to the contents of the actual partitions on the
  656. * device. It is not used to access the GPT.
  657. */
  658. VbError_t VbExStreamRead(VbExStream_t stream, uint32_t bytes, void *buffer);
  659. /**
  660. * Close a stream
  661. *
  662. * @param stream Stream to close
  663. */
  664. void VbExStreamClose(VbExStream_t stream);
  665. /*****************************************************************************/
  666. /* Display */
  667. /* Predefined (default) screens for VbExDisplayScreen(). */
  668. enum VbScreenType_t {
  669. /* Blank (clear) screen */
  670. VB_SCREEN_BLANK = 0,
  671. /* Developer - warning */
  672. VB_SCREEN_DEVELOPER_WARNING = 0x101,
  673. /* Developer - easter egg */
  674. VB_SCREEN_DEVELOPER_EGG = 0x102,
  675. /* Recovery - remove inserted devices */
  676. VB_SCREEN_RECOVERY_REMOVE = 0x201,
  677. /* Recovery - insert recovery image */
  678. VB_SCREEN_RECOVERY_INSERT = 0x202,
  679. /* Recovery - inserted image invalid */
  680. VB_SCREEN_RECOVERY_NO_GOOD = 0x203,
  681. /* Recovery - confirm dev mode */
  682. VB_SCREEN_RECOVERY_TO_DEV = 0x204,
  683. /* Developer - confirm normal mode */
  684. VB_SCREEN_DEVELOPER_TO_NORM = 0x205,
  685. /* Please wait - programming EC */
  686. VB_SCREEN_WAIT = 0x206,
  687. /* Confirm after DEVELOPER_TO_NORM */
  688. VB_SCREEN_TO_NORM_CONFIRMED = 0x207,
  689. /* Broken screen shown after verification failure */
  690. VB_SCREEN_OS_BROKEN = 0x208,
  691. };
  692. /**
  693. * Initialize and clear the display. Set width and height to the screen
  694. * dimensions in pixels.
  695. */
  696. VbError_t VbExDisplayInit(uint32_t *width, uint32_t *height);
  697. /**
  698. * Enable (enable!=0) or disable (enable=0) the display backlight.
  699. */
  700. VbError_t VbExDisplayBacklight(uint8_t enable);
  701. /**
  702. * Sets the logical dimension to display.
  703. *
  704. * If the physical display is larger or smaller than given dimension, display
  705. * provider may decide to scale or shift images (from VbExDisplayImage)to proper
  706. * location.
  707. */
  708. VbError_t VbExDisplaySetDimension(uint32_t width, uint32_t height);
  709. /**
  710. * Display a predefined screen; see VB_SCREEN_* for valid screens.
  711. *
  712. * This is a backup method of screen display, intended for use if the GBB does
  713. * not contain a full set of bitmaps. It is acceptable for the backup screen
  714. * to be simple ASCII text such as "NO GOOD" or "INSERT"; these screens should
  715. * only be seen during development.
  716. */
  717. VbError_t VbExDisplayScreen(uint32_t screen_type, uint32_t locale);
  718. /**
  719. * Write an image to the display, with the upper left corner at the specified
  720. * pixel coordinates. The bitmap buffer is a pointer to the platform-dependent
  721. * uncompressed binary blob with dimensions and format specified internally
  722. * (for example, a raw BMP, GIF, PNG, whatever). We pass the size just for
  723. * convenience.
  724. */
  725. VbError_t VbExDisplayImage(uint32_t x, uint32_t y,
  726. void *buffer, uint32_t buffersize);
  727. /**
  728. * Display a string at coordinate x,y
  729. */
  730. VbError_t VbExDisplayText(uint32_t x, uint32_t y,
  731. const char *info_str);
  732. /**
  733. * Display a string containing debug information on the screen, rendered in a
  734. * platform-dependent font. Should be able to handle newlines '\n' in the
  735. * string. Firmware must support displaying at least 20 lines of text, where
  736. * each line may be at least 80 characters long. If the firmware has its own
  737. * debug state, it may display it to the screen below this information.
  738. *
  739. * NOTE: This is what we currently display when TAB is pressed. Some
  740. * information (HWID, recovery reason) is ours; some (CMOS breadcrumbs) is
  741. * platform-specific. If we decide to soft-render the HWID string
  742. * (chrome-os-partner:3693), we'll need to maintain our own fonts, so we'll
  743. * likely display it via VbExDisplayImage() above.
  744. */
  745. VbError_t VbExDisplayDebugInfo(const char *info_str);
  746. /*****************************************************************************/
  747. /* Keyboard and switches */
  748. /* Key codes for required non-printable-ASCII characters. */
  749. enum VbKeyCode_t {
  750. VB_KEY_UP = 0x100,
  751. VB_KEY_DOWN = 0x101,
  752. VB_KEY_LEFT = 0x102,
  753. VB_KEY_RIGHT = 0x103,
  754. VB_KEY_CTRL_ENTER = 0x104,
  755. };
  756. /* Flags for additional information.
  757. * TODO(semenzato): consider adding flags for modifiers instead of
  758. * making up some of the key codes above.
  759. */
  760. enum VbKeyFlags_t {
  761. VB_KEY_FLAG_TRUSTED_KEYBOARD = 1 << 0,
  762. };
  763. /**
  764. * Read the next keypress from the keyboard buffer.
  765. *
  766. * Returns the keypress, or zero if no keypress is pending or error.
  767. *
  768. * The following keys must be returned as ASCII character codes:
  769. * 0x08 Backspace
  770. * 0x09 Tab
  771. * 0x0D Enter (carriage return)
  772. * 0x01 - 0x1A Ctrl+A - Ctrl+Z (yes, those alias with backspace/tab/enter)
  773. * 0x1B Esc
  774. * 0x20 Space
  775. * 0x30 - 0x39 '0' - '9'
  776. * 0x60 - 0x7A 'a' - 'z'
  777. *
  778. * Some extended keys must also be supported; see the VB_KEY_* defines above.
  779. *
  780. * Keys ('/') or key-chords (Fn+Q) not defined above may be handled in any of
  781. * the following ways:
  782. * 1. Filter (don't report anything if one of these keys is pressed).
  783. * 2. Report as ASCII (if a well-defined ASCII value exists for the key).
  784. * 3. Report as any other value in the range 0x200 - 0x2FF.
  785. * It is not permitted to report a key as a multi-byte code (for example,
  786. * sending an arrow key as the sequence of keys '\x1b', '[', '1', 'A'). */
  787. uint32_t VbExKeyboardRead(void);
  788. /**
  789. * Same as VbExKeyboardRead(), but return extra information.
  790. */
  791. uint32_t VbExKeyboardReadWithFlags(uint32_t *flags_ptr);
  792. /**
  793. * Return the current state of the switches specified in request_mask
  794. */
  795. uint32_t VbExGetSwitches(uint32_t request_mask);
  796. /*****************************************************************************/
  797. /* Embedded controller (EC) */
  798. /*
  799. * All these functions take a devidx parameter, which indicates which embedded
  800. * processor the call applies to. At present, only devidx=0 is valid, but
  801. * upcoming CLs will add support for multiple devices.
  802. */
  803. /**
  804. * This is called only if the system implements a keyboard-based (virtual)
  805. * developer switch. It must return true only if the system has an embedded
  806. * controller which is provably running in its RO firmware at the time the
  807. * function is called.
  808. */
  809. int VbExTrustEC(int devidx);
  810. /**
  811. * Check if the EC is currently running rewritable code.
  812. *
  813. * If the EC is in RO code, sets *in_rw=0.
  814. * If the EC is in RW code, sets *in_rw non-zero.
  815. * If the current EC image is unknown, returns error. */
  816. VbError_t VbExEcRunningRW(int devidx, int *in_rw);
  817. /**
  818. * Request the EC jump to its rewritable code. If successful, returns when the
  819. * EC has booting its RW code far enough to respond to subsequent commands.
  820. * Does nothing if the EC is already in its rewritable code.
  821. */
  822. VbError_t VbExEcJumpToRW(int devidx);
  823. /**
  824. * Tell the EC to refuse another jump until it reboots. Subsequent calls to
  825. * VbExEcJumpToRW() in this boot will fail.
  826. */
  827. VbError_t VbExEcDisableJump(int devidx);
  828. /**
  829. * Read the SHA-256 hash of the selected EC image.
  830. */
  831. VbError_t VbExEcHashImage(int devidx, enum VbSelectFirmware_t select,
  832. const uint8_t **hash, int *hash_size);
  833. /**
  834. * Get the expected contents of the EC image associated with the main firmware
  835. * specified by the "select" argument.
  836. */
  837. VbError_t VbExEcGetExpectedImage(int devidx, enum VbSelectFirmware_t select,
  838. const uint8_t **image, int *image_size);
  839. /**
  840. * Read the SHA-256 hash of the expected contents of the EC image associated
  841. * with the main firmware specified by the "select" argument.
  842. */
  843. VbError_t VbExEcGetExpectedImageHash(int devidx, enum VbSelectFirmware_t select,
  844. const uint8_t **hash, int *hash_size);
  845. /**
  846. * Update the selected EC image.
  847. */
  848. VbError_t VbExEcUpdateImage(int devidx, enum VbSelectFirmware_t select,
  849. const uint8_t *image, int image_size);
  850. /**
  851. * Lock the selected EC code to prevent updates until the EC is rebooted.
  852. * Subsequent calls to VbExEcUpdateImage() with the same region this boot will
  853. * fail.
  854. */
  855. VbError_t VbExEcProtect(int devidx, enum VbSelectFirmware_t select);
  856. /**
  857. * Info the EC of the boot mode selected by the AP.
  858. * mode: Normal, Developer, or Recovery
  859. */
  860. enum VbEcBootMode_t {VB_EC_NORMAL, VB_EC_DEVELOPER, VB_EC_RECOVERY };
  861. VbError_t VbExEcEnteringMode(int devidx, enum VbEcBootMode_t mode);
  862. /**
  863. * Perform EC post-verification / updating / jumping actions.
  864. *
  865. * This routine is called to perform certain actions that must wait until
  866. * after the EC resides in its `final` image (the image the EC will
  867. * run for the duration of boot). These actions include verifying that
  868. * enough power is available to continue with boot.
  869. *
  870. * @param in_recovery 1 if recovery mode is selected by the AP, 0 otherwise.
  871. * @return VBERROR_... error, VBERROR_SUCCESS on success.
  872. */
  873. VbError_t VbExEcVbootDone(int in_recovery);
  874. /**
  875. * Request EC to stop discharging and cut-off battery.
  876. */
  877. VbError_t VbExEcBatteryCutOff(void);
  878. /*****************************************************************************/
  879. /* Misc */
  880. /* Args to VbExProtectFlash() */
  881. enum VbProtectFlash_t { VBPROTECT_RW_A, VBPROTECT_RW_B, VBPROTECT_RW_DEVKEY };
  882. /**
  883. * Lock a section of the BIOS flash address space to prevent updates until the
  884. * host is rebooted. Subsequent attempts to erase or modify the specified BIOS
  885. * image will fail. If this function is called more than once each call should
  886. * be cumulative.
  887. */
  888. VbError_t VbExProtectFlash(enum VbProtectFlash_t region);
  889. /**
  890. * Check if the firmware needs to shut down the system.
  891. *
  892. * Returns a non-zero VB_SHUTDOWN_REQUEST mask indicating the reason(s) for
  893. * shutdown if a shutdown is being requested (see VB_SHUTDOWN_REQUEST_*), or 0
  894. * if a shutdown is not being requested.
  895. *
  896. * NOTE: When we're displaying a screen, pressing the power button should shut
  897. * down the computer. We need a way to break out of our control loop so this
  898. * can occur cleanly.
  899. */
  900. uint32_t VbExIsShutdownRequested(void);
  901. /*
  902. * Shutdown requested for a reason which is not defined among other
  903. * VB_SHUTDOWN_REQUEST_* values. This must be defined as 1 for backward
  904. * compatibility with old versions of the API.
  905. */
  906. #define VB_SHUTDOWN_REQUEST_OTHER 0x00000001
  907. /* Shutdown requested due to a lid switch being closed. */
  908. #define VB_SHUTDOWN_REQUEST_LID_CLOSED 0x00000002
  909. /* Shutdown requested due to a power button being pressed. */
  910. #define VB_SHUTDOWN_REQUEST_POWER_BUTTON 0x00000004
  911. /**
  912. * Expose the BIOS' built-in decompression routine to the vboot wrapper. The
  913. * caller must know how large the uncompressed data will be and must manage
  914. * that memory. The decompression routine just puts the uncompressed data into
  915. * the specified buffer. We pass in the size of the outbuf, and get back the
  916. * actual size used.
  917. */
  918. VbError_t VbExDecompress(void *inbuf, uint32_t in_size,
  919. uint32_t compression_type,
  920. void *outbuf, uint32_t *out_size);
  921. /* Constants for compression_type */
  922. enum {
  923. COMPRESS_NONE = 0,
  924. COMPRESS_EFIv1, /* The x86 BIOS only supports this */
  925. COMPRESS_LZMA1, /* The ARM BIOS supports LZMA1 */
  926. MAX_COMPRESS,
  927. };
  928. /**
  929. * Execute legacy boot option.
  930. */
  931. int VbExLegacy(void);
  932. /* Regions for VbExRegionRead() */
  933. enum vb_firmware_region {
  934. VB_REGION_GBB, /* Google Binary Block - see gbbheader.h */
  935. VB_REGION_COUNT,
  936. };
  937. /**
  938. * Read data from a region of the firmware image
  939. *
  940. * Vboot wants access to a region, to read data from it. This function
  941. * reads it (typically from the firmware image such as SPI flash) and
  942. * returns the data.
  943. *
  944. * cparams is passed so that the boot loader has some context for the
  945. * operation.
  946. *
  947. * @param cparams Common parameters, e.g. use member caller_context
  948. * to point to useful context data
  949. * @param region Firmware region to read
  950. * @param offset Start offset within region
  951. * @param size Number of bytes to read
  952. * @param buf Place to put data
  953. * @return VBERROR_... error, VBERROR_SUCCESS on success,
  954. */
  955. VbError_t VbExRegionRead(VbCommonParams *cparams,
  956. enum vb_firmware_region region, uint32_t offset,
  957. uint32_t size, void *buf);
  958. /**
  959. * Check if the firmware wants to override GPT entry priority.
  960. *
  961. * In case of kernel entry, check if there is an override of priority
  962. * available. This is used to select a particular partition to boot in the
  963. * current boot cycle. Rollback protection, image verification and all other
  964. * checks in LoadKernel still remain the same.
  965. *
  966. * @param e Gpt Entry to check for priority override.
  967. * @return 0 if no override, 1-15 for override priority.
  968. */
  969. uint8_t VbExOverrideGptEntryPriority(const GptEntry *e);
  970. /**
  971. * Return number of locales supported
  972. *
  973. * @param count Pointer to the number of locales.
  974. * @return VBERROR_... error, VBERROR_SUCCESS on success.
  975. */
  976. VbError_t VbExGetLocalizationCount(uint32_t *count);
  977. #endif /* VBOOT_REFERENCE_VBOOT_API_H_ */