controlcenterd-id.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2013
  4. * Reinhard Pfau, Guntermann & Drunck GmbH, reinhard.pfau@gdsys.cc
  5. */
  6. /* TODO: some more #ifdef's to avoid unneeded code for stage 1 / stage 2 */
  7. #ifdef CCDM_ID_DEBUG
  8. #define DEBUG
  9. #endif
  10. #include <common.h>
  11. #include <bootstage.h>
  12. #include <command.h>
  13. #include <dm.h>
  14. #include <env.h>
  15. #include <hang.h>
  16. #include <log.h>
  17. #include <malloc.h>
  18. #include <fs.h>
  19. #include <i2c.h>
  20. #include <mmc.h>
  21. #include <tpm-v1.h>
  22. #include <linux/delay.h>
  23. #include <u-boot/crc.h>
  24. #include <u-boot/sha1.h>
  25. #include <asm/byteorder.h>
  26. #include <asm/unaligned.h>
  27. #include <pca9698.h>
  28. #undef CCDM_FIRST_STAGE
  29. #undef CCDM_SECOND_STAGE
  30. #undef CCDM_AUTO_FIRST_STAGE
  31. #ifdef CONFIG_DEVELOP
  32. #define CCDM_DEVELOP
  33. #endif
  34. #ifdef CONFIG_TRAILBLAZER
  35. #define CCDM_FIRST_STAGE
  36. #undef CCDM_SECOND_STAGE
  37. #else
  38. #undef CCDM_FIRST_STAGE
  39. #define CCDM_SECOND_STAGE
  40. #endif
  41. #if defined(CCDM_DEVELOP) && defined(CCDM_SECOND_STAGE) && \
  42. !defined(CCCM_FIRST_STAGE)
  43. #define CCDM_AUTO_FIRST_STAGE
  44. #endif
  45. /* CCDM specific contants */
  46. enum {
  47. /* NV indices */
  48. NV_COMMON_DATA_INDEX = 0x40000001,
  49. /* magics for key blob chains */
  50. MAGIC_KEY_PROGRAM = 0x68726500,
  51. MAGIC_HMAC = 0x68616300,
  52. MAGIC_END_OF_CHAIN = 0x00000000,
  53. /* sizes */
  54. NV_COMMON_DATA_MIN_SIZE = 3 * sizeof(uint64_t) + 2 * sizeof(uint16_t),
  55. };
  56. /* other constants */
  57. enum {
  58. ESDHC_BOOT_IMAGE_SIG_OFS = 0x40,
  59. ESDHC_BOOT_IMAGE_SIZE_OFS = 0x48,
  60. ESDHC_BOOT_IMAGE_ADDR_OFS = 0x50,
  61. ESDHC_BOOT_IMAGE_TARGET_OFS = 0x58,
  62. ESDHC_BOOT_IMAGE_ENTRY_OFS = 0x60,
  63. };
  64. enum {
  65. I2C_SOC_0 = 0,
  66. I2C_SOC_1 = 1,
  67. };
  68. struct key_program {
  69. uint32_t magic;
  70. uint32_t code_crc;
  71. uint32_t code_size;
  72. uint8_t code[];
  73. };
  74. struct h_reg {
  75. bool valid;
  76. uint8_t digest[20];
  77. };
  78. enum access_mode {
  79. HREG_NONE = 0,
  80. HREG_RD = 1,
  81. HREG_WR = 2,
  82. HREG_RDWR = 3,
  83. };
  84. /* register constants */
  85. enum {
  86. FIX_HREG_DEVICE_ID_HASH = 0,
  87. FIX_HREG_SELF_HASH = 1,
  88. FIX_HREG_STAGE2_HASH = 2,
  89. FIX_HREG_VENDOR = 3,
  90. COUNT_FIX_HREGS
  91. };
  92. /* hre opcodes */
  93. enum {
  94. /* opcodes w/o data */
  95. HRE_NOP = 0x00,
  96. HRE_SYNC = HRE_NOP,
  97. HRE_CHECK0 = 0x01,
  98. /* opcodes w/o data, w/ sync dst */
  99. /* opcodes w/ data */
  100. HRE_LOAD = 0x81,
  101. /* opcodes w/data, w/sync dst */
  102. HRE_XOR = 0xC1,
  103. HRE_AND = 0xC2,
  104. HRE_OR = 0xC3,
  105. HRE_EXTEND = 0xC4,
  106. HRE_LOADKEY = 0xC5,
  107. };
  108. /* hre errors */
  109. enum {
  110. HRE_E_OK = 0,
  111. HRE_E_TPM_FAILURE,
  112. HRE_E_INVALID_HREG,
  113. };
  114. static uint64_t device_id;
  115. static uint64_t device_cl;
  116. static uint64_t device_type;
  117. static uint32_t platform_key_handle;
  118. static void(*bl2_entry)(void);
  119. static struct h_reg pcr_hregs[24];
  120. static struct h_reg fix_hregs[COUNT_FIX_HREGS];
  121. static struct h_reg var_hregs[8];
  122. static uint32_t hre_tpm_err;
  123. static int hre_err = HRE_E_OK;
  124. #define IS_PCR_HREG(spec) ((spec) & 0x20)
  125. #define IS_FIX_HREG(spec) (((spec) & 0x38) == 0x08)
  126. #define IS_VAR_HREG(spec) (((spec) & 0x38) == 0x10)
  127. #define HREG_IDX(spec) ((spec) & (IS_PCR_HREG(spec) ? 0x1f : 0x7))
  128. static int get_tpm(struct udevice **devp)
  129. {
  130. int rc;
  131. rc = uclass_first_device_err(UCLASS_TPM, devp);
  132. if (rc) {
  133. printf("Could not find TPM (ret=%d)\n", rc);
  134. return CMD_RET_FAILURE;
  135. }
  136. return 0;
  137. }
  138. static const uint8_t vendor[] = "Guntermann & Drunck";
  139. /**
  140. * @brief read a bunch of data from MMC into memory.
  141. *
  142. * @param mmc pointer to the mmc structure to use.
  143. * @param src offset where the data starts on MMC/SD device (in bytes).
  144. * @param dst pointer to the location where the read data should be stored.
  145. * @param size number of bytes to read from the MMC/SD device.
  146. * @return number of bytes read or -1 on error.
  147. */
  148. static int ccdm_mmc_read(struct mmc *mmc, u64 src, u8 *dst, int size)
  149. {
  150. int result = 0;
  151. u32 blk_len, ofs;
  152. ulong block_no, n, cnt;
  153. u8 *tmp_buf = NULL;
  154. if (size <= 0)
  155. goto end;
  156. blk_len = mmc->read_bl_len;
  157. tmp_buf = malloc(blk_len);
  158. if (!tmp_buf)
  159. goto failure;
  160. block_no = src / blk_len;
  161. ofs = src % blk_len;
  162. if (ofs) {
  163. n = mmc->block_dev.block_read(&mmc->block_dev, block_no++, 1,
  164. tmp_buf);
  165. if (!n)
  166. goto failure;
  167. result = min(size, (int)(blk_len - ofs));
  168. memcpy(dst, tmp_buf + ofs, result);
  169. dst += result;
  170. size -= result;
  171. }
  172. cnt = size / blk_len;
  173. if (cnt) {
  174. n = mmc->block_dev.block_read(&mmc->block_dev, block_no, cnt,
  175. dst);
  176. if (n != cnt)
  177. goto failure;
  178. size -= cnt * blk_len;
  179. result += cnt * blk_len;
  180. dst += cnt * blk_len;
  181. block_no += cnt;
  182. }
  183. if (size) {
  184. n = mmc->block_dev.block_read(&mmc->block_dev, block_no++, 1,
  185. tmp_buf);
  186. if (!n)
  187. goto failure;
  188. memcpy(dst, tmp_buf, size);
  189. result += size;
  190. }
  191. goto end;
  192. failure:
  193. result = -1;
  194. end:
  195. if (tmp_buf)
  196. free(tmp_buf);
  197. return result;
  198. }
  199. /**
  200. * @brief returns a location where the 2nd stage bootloader can be(/ is) placed.
  201. *
  202. * @return pointer to the location for/of the 2nd stage bootloader
  203. */
  204. static u8 *get_2nd_stage_bl_location(ulong target_addr)
  205. {
  206. ulong addr;
  207. #ifdef CCDM_SECOND_STAGE
  208. addr = env_get_ulong("loadaddr", 16, CONFIG_LOADADDR);
  209. #else
  210. addr = target_addr;
  211. #endif
  212. return (u8 *)(addr);
  213. }
  214. #ifdef CCDM_SECOND_STAGE
  215. /**
  216. * @brief returns a location where the image can be(/ is) placed.
  217. *
  218. * @return pointer to the location for/of the image
  219. */
  220. static u8 *get_image_location(void)
  221. {
  222. ulong addr;
  223. /* TODO use other area? */
  224. addr = env_get_ulong("loadaddr", 16, CONFIG_LOADADDR);
  225. return (u8 *)(addr);
  226. }
  227. #endif
  228. /**
  229. * @brief get the size of a given (TPM) NV area
  230. * @param index NV index of the area to get size for
  231. * @param size pointer to the size
  232. * @return 0 on success, != 0 on error
  233. */
  234. static int get_tpm_nv_size(struct udevice *tpm, uint32_t index, uint32_t *size)
  235. {
  236. uint32_t err;
  237. uint8_t info[72];
  238. uint8_t *ptr;
  239. uint16_t v16;
  240. err = tpm1_get_capability(tpm, TPM_CAP_NV_INDEX, index, info,
  241. sizeof(info));
  242. if (err) {
  243. printf("tpm_get_capability(CAP_NV_INDEX, %08x) failed: %u\n",
  244. index, err);
  245. return 1;
  246. }
  247. /* skip tag and nvIndex */
  248. ptr = info + 6;
  249. /* skip 2 pcr info fields */
  250. v16 = get_unaligned_be16(ptr);
  251. ptr += 2 + v16 + 1 + 20;
  252. v16 = get_unaligned_be16(ptr);
  253. ptr += 2 + v16 + 1 + 20;
  254. /* skip permission and flags */
  255. ptr += 6 + 3;
  256. *size = get_unaligned_be32(ptr);
  257. return 0;
  258. }
  259. /**
  260. * @brief search for a key by usage auth and pub key hash.
  261. * @param auth usage auth of the key to search for
  262. * @param pubkey_digest (SHA1) hash of the pub key structure of the key
  263. * @param[out] handle the handle of the key iff found
  264. * @return 0 if key was found in TPM; != 0 if not.
  265. */
  266. static int find_key(struct udevice *tpm, const uint8_t auth[20],
  267. const uint8_t pubkey_digest[20], uint32_t *handle)
  268. {
  269. uint16_t key_count;
  270. uint32_t key_handles[10];
  271. uint8_t buf[288];
  272. uint8_t *ptr;
  273. uint32_t err;
  274. uint8_t digest[20];
  275. size_t buf_len;
  276. unsigned int i;
  277. /* fetch list of already loaded keys in the TPM */
  278. err = tpm1_get_capability(tpm, TPM_CAP_HANDLE, TPM_RT_KEY, buf,
  279. sizeof(buf));
  280. if (err)
  281. return -1;
  282. key_count = get_unaligned_be16(buf);
  283. ptr = buf + 2;
  284. for (i = 0; i < key_count; ++i, ptr += 4)
  285. key_handles[i] = get_unaligned_be32(ptr);
  286. /* now search a(/ the) key which we can access with the given auth */
  287. for (i = 0; i < key_count; ++i) {
  288. buf_len = sizeof(buf);
  289. err = tpm1_get_pub_key_oiap(tpm, key_handles[i], auth, buf,
  290. &buf_len);
  291. if (err && err != TPM_AUTHFAIL)
  292. return -1;
  293. if (err)
  294. continue;
  295. sha1_csum(buf, buf_len, digest);
  296. if (!memcmp(digest, pubkey_digest, 20)) {
  297. *handle = key_handles[i];
  298. return 0;
  299. }
  300. }
  301. return 1;
  302. }
  303. /**
  304. * @brief read CCDM common data from TPM NV
  305. * @return 0 if CCDM common data was found and read, !=0 if something failed.
  306. */
  307. static int read_common_data(struct udevice *tpm)
  308. {
  309. uint32_t size;
  310. uint32_t err;
  311. uint8_t buf[256];
  312. sha1_context ctx;
  313. if (get_tpm_nv_size(tpm, NV_COMMON_DATA_INDEX, &size) ||
  314. size < NV_COMMON_DATA_MIN_SIZE)
  315. return 1;
  316. err = tpm1_nv_read_value(tpm, NV_COMMON_DATA_INDEX, buf,
  317. min(sizeof(buf), size));
  318. if (err) {
  319. printf("tpm_nv_read_value() failed: %u\n", err);
  320. return 1;
  321. }
  322. device_id = get_unaligned_be64(buf);
  323. device_cl = get_unaligned_be64(buf + 8);
  324. device_type = get_unaligned_be64(buf + 16);
  325. sha1_starts(&ctx);
  326. sha1_update(&ctx, buf, 24);
  327. sha1_finish(&ctx, fix_hregs[FIX_HREG_DEVICE_ID_HASH].digest);
  328. fix_hregs[FIX_HREG_DEVICE_ID_HASH].valid = true;
  329. platform_key_handle = get_unaligned_be32(buf + 24);
  330. return 0;
  331. }
  332. /**
  333. * @brief compute hash of bootloader itself.
  334. * @param[out] dst hash register where the hash should be stored
  335. * @return 0 on success, != 0 on failure.
  336. *
  337. * @note MUST be called at a time where the boot loader is accessible at the
  338. * configured location (; so take care when code is reallocated).
  339. */
  340. static int compute_self_hash(struct h_reg *dst)
  341. {
  342. sha1_csum((const uint8_t *)CONFIG_SYS_MONITOR_BASE,
  343. CONFIG_SYS_MONITOR_LEN, dst->digest);
  344. dst->valid = true;
  345. return 0;
  346. }
  347. int ccdm_compute_self_hash(void)
  348. {
  349. if (!fix_hregs[FIX_HREG_SELF_HASH].valid)
  350. compute_self_hash(&fix_hregs[FIX_HREG_SELF_HASH]);
  351. return 0;
  352. }
  353. /**
  354. * @brief compute the hash of the 2nd stage boot loader (on SD card)
  355. * @param[out] dst hash register to store the computed hash
  356. * @return 0 on success, != 0 on failure
  357. *
  358. * Determines the size and location of the 2nd stage boot loader on SD card,
  359. * loads the 2nd stage boot loader and computes the (SHA1) hash value.
  360. * Within the 1st stage boot loader, the 2nd stage boot loader is loaded at
  361. * the desired memory location and the variable @a bl2_entry is set.
  362. *
  363. * @note This sets the variable @a bl2_entry to the entry point when the
  364. * 2nd stage boot loader is loaded at its configured memory location.
  365. */
  366. static int compute_second_stage_hash(struct h_reg *dst)
  367. {
  368. int result = 0;
  369. u32 code_len, code_offset, target_addr, exec_entry;
  370. struct mmc *mmc;
  371. u8 *load_addr = NULL;
  372. u8 buf[128];
  373. mmc = find_mmc_device(0);
  374. if (!mmc)
  375. goto failure;
  376. mmc_init(mmc);
  377. if (ccdm_mmc_read(mmc, 0, buf, sizeof(buf)) < 0)
  378. goto failure;
  379. code_offset = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ADDR_OFS);
  380. code_len = *(u32 *)(buf + ESDHC_BOOT_IMAGE_SIZE_OFS);
  381. target_addr = *(u32 *)(buf + ESDHC_BOOT_IMAGE_TARGET_OFS);
  382. exec_entry = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ENTRY_OFS);
  383. load_addr = get_2nd_stage_bl_location(target_addr);
  384. if (load_addr == (u8 *)target_addr)
  385. bl2_entry = (void(*)(void))exec_entry;
  386. if (ccdm_mmc_read(mmc, code_offset, load_addr, code_len) < 0)
  387. goto failure;
  388. sha1_csum(load_addr, code_len, dst->digest);
  389. dst->valid = true;
  390. goto end;
  391. failure:
  392. result = 1;
  393. bl2_entry = NULL;
  394. end:
  395. return result;
  396. }
  397. /**
  398. * @brief get pointer to hash register by specification
  399. * @param spec specification of a hash register
  400. * @return pointer to hash register or NULL if @a spec does not qualify a
  401. * valid hash register; NULL else.
  402. */
  403. static struct h_reg *get_hreg(uint8_t spec)
  404. {
  405. uint8_t idx;
  406. idx = HREG_IDX(spec);
  407. if (IS_FIX_HREG(spec)) {
  408. if (idx < ARRAY_SIZE(fix_hregs))
  409. return fix_hregs + idx;
  410. hre_err = HRE_E_INVALID_HREG;
  411. } else if (IS_PCR_HREG(spec)) {
  412. if (idx < ARRAY_SIZE(pcr_hregs))
  413. return pcr_hregs + idx;
  414. hre_err = HRE_E_INVALID_HREG;
  415. } else if (IS_VAR_HREG(spec)) {
  416. if (idx < ARRAY_SIZE(var_hregs))
  417. return var_hregs + idx;
  418. hre_err = HRE_E_INVALID_HREG;
  419. }
  420. return NULL;
  421. }
  422. /**
  423. * @brief get pointer of a hash register by specification and usage.
  424. * @param spec specification of a hash register
  425. * @param mode access mode (read or write or read/write)
  426. * @return pointer to hash register if found and valid; NULL else.
  427. *
  428. * This func uses @a get_reg() to determine the hash register for a given spec.
  429. * If a register is found it is validated according to the desired access mode.
  430. * The value of automatic registers (PCR register and fixed registers) is
  431. * loaded or computed on read access.
  432. */
  433. static struct h_reg *access_hreg(struct udevice *tpm, uint8_t spec,
  434. enum access_mode mode)
  435. {
  436. struct h_reg *result;
  437. result = get_hreg(spec);
  438. if (!result)
  439. return NULL;
  440. if (mode & HREG_WR) {
  441. if (IS_FIX_HREG(spec)) {
  442. hre_err = HRE_E_INVALID_HREG;
  443. return NULL;
  444. }
  445. }
  446. if (mode & HREG_RD) {
  447. if (!result->valid) {
  448. if (IS_PCR_HREG(spec)) {
  449. hre_tpm_err = tpm1_pcr_read(tpm, HREG_IDX(spec),
  450. result->digest, 20);
  451. result->valid = (hre_tpm_err == TPM_SUCCESS);
  452. } else if (IS_FIX_HREG(spec)) {
  453. switch (HREG_IDX(spec)) {
  454. case FIX_HREG_DEVICE_ID_HASH:
  455. read_common_data(tpm);
  456. break;
  457. case FIX_HREG_SELF_HASH:
  458. ccdm_compute_self_hash();
  459. break;
  460. case FIX_HREG_STAGE2_HASH:
  461. compute_second_stage_hash(result);
  462. break;
  463. case FIX_HREG_VENDOR:
  464. memcpy(result->digest, vendor, 20);
  465. result->valid = true;
  466. break;
  467. }
  468. } else {
  469. result->valid = true;
  470. }
  471. }
  472. if (!result->valid) {
  473. hre_err = HRE_E_INVALID_HREG;
  474. return NULL;
  475. }
  476. }
  477. return result;
  478. }
  479. static void *compute_and(void *_dst, const void *_src, size_t n)
  480. {
  481. uint8_t *dst = _dst;
  482. const uint8_t *src = _src;
  483. size_t i;
  484. for (i = n; i-- > 0; )
  485. *dst++ &= *src++;
  486. return _dst;
  487. }
  488. static void *compute_or(void *_dst, const void *_src, size_t n)
  489. {
  490. uint8_t *dst = _dst;
  491. const uint8_t *src = _src;
  492. size_t i;
  493. for (i = n; i-- > 0; )
  494. *dst++ |= *src++;
  495. return _dst;
  496. }
  497. static void *compute_xor(void *_dst, const void *_src, size_t n)
  498. {
  499. uint8_t *dst = _dst;
  500. const uint8_t *src = _src;
  501. size_t i;
  502. for (i = n; i-- > 0; )
  503. *dst++ ^= *src++;
  504. return _dst;
  505. }
  506. static void *compute_extend(void *_dst, const void *_src, size_t n)
  507. {
  508. uint8_t digest[20];
  509. sha1_context ctx;
  510. sha1_starts(&ctx);
  511. sha1_update(&ctx, _dst, n);
  512. sha1_update(&ctx, _src, n);
  513. sha1_finish(&ctx, digest);
  514. memcpy(_dst, digest, min(n, sizeof(digest)));
  515. return _dst;
  516. }
  517. static int hre_op_loadkey(struct udevice *tpm, struct h_reg *src_reg,
  518. struct h_reg *dst_reg, const void *key,
  519. size_t key_size)
  520. {
  521. uint32_t parent_handle;
  522. uint32_t key_handle;
  523. if (!src_reg || !dst_reg || !src_reg->valid || !dst_reg->valid)
  524. return -1;
  525. if (find_key(tpm, src_reg->digest, dst_reg->digest, &parent_handle))
  526. return -1;
  527. hre_tpm_err = tpm1_load_key2_oiap(tpm, parent_handle, key, key_size,
  528. src_reg->digest, &key_handle);
  529. if (hre_tpm_err) {
  530. hre_err = HRE_E_TPM_FAILURE;
  531. return -1;
  532. }
  533. /* TODO remember key handle somehow? */
  534. return 0;
  535. }
  536. /**
  537. * @brief executes the next opcode on the hash register engine.
  538. * @param[in,out] ip pointer to the opcode (instruction pointer)
  539. * @param[in,out] code_size (remaining) size of the code
  540. * @return new instruction pointer on success, NULL on error.
  541. */
  542. static const uint8_t *hre_execute_op(struct udevice *tpm, const uint8_t **ip,
  543. size_t *code_size)
  544. {
  545. bool dst_modified = false;
  546. uint32_t ins;
  547. uint8_t opcode;
  548. uint8_t src_spec;
  549. uint8_t dst_spec;
  550. uint16_t data_size;
  551. struct h_reg *src_reg, *dst_reg;
  552. uint8_t buf[20];
  553. const uint8_t *src_buf, *data;
  554. uint8_t *ptr;
  555. int i;
  556. void * (*bin_func)(void *, const void *, size_t);
  557. if (*code_size < 4)
  558. return NULL;
  559. ins = get_unaligned_be32(*ip);
  560. opcode = **ip;
  561. data = *ip + 4;
  562. src_spec = (ins >> 18) & 0x3f;
  563. dst_spec = (ins >> 12) & 0x3f;
  564. data_size = (ins & 0x7ff);
  565. debug("HRE: ins=%08x (op=%02x, s=%02x, d=%02x, L=%d)\n", ins,
  566. opcode, src_spec, dst_spec, data_size);
  567. if ((opcode & 0x80) && (data_size + 4) > *code_size)
  568. return NULL;
  569. src_reg = access_hreg(tpm, src_spec, HREG_RD);
  570. if (hre_err || hre_tpm_err)
  571. return NULL;
  572. dst_reg = access_hreg(tpm, dst_spec,
  573. (opcode & 0x40) ? HREG_RDWR : HREG_WR);
  574. if (hre_err || hre_tpm_err)
  575. return NULL;
  576. switch (opcode) {
  577. case HRE_NOP:
  578. goto end;
  579. case HRE_CHECK0:
  580. if (src_reg) {
  581. for (i = 0; i < 20; ++i) {
  582. if (src_reg->digest[i])
  583. return NULL;
  584. }
  585. }
  586. break;
  587. case HRE_LOAD:
  588. bin_func = memcpy;
  589. goto do_bin_func;
  590. case HRE_XOR:
  591. bin_func = compute_xor;
  592. goto do_bin_func;
  593. case HRE_AND:
  594. bin_func = compute_and;
  595. goto do_bin_func;
  596. case HRE_OR:
  597. bin_func = compute_or;
  598. goto do_bin_func;
  599. case HRE_EXTEND:
  600. bin_func = compute_extend;
  601. do_bin_func:
  602. if (!dst_reg)
  603. return NULL;
  604. if (src_reg) {
  605. src_buf = src_reg->digest;
  606. } else {
  607. if (!data_size) {
  608. memset(buf, 0, 20);
  609. src_buf = buf;
  610. } else if (data_size == 1) {
  611. memset(buf, *data, 20);
  612. src_buf = buf;
  613. } else if (data_size >= 20) {
  614. src_buf = data;
  615. } else {
  616. src_buf = buf;
  617. for (ptr = (uint8_t *)src_buf, i = 20; i > 0;
  618. i -= data_size, ptr += data_size)
  619. memcpy(ptr, data,
  620. min_t(size_t, i, data_size));
  621. }
  622. }
  623. bin_func(dst_reg->digest, src_buf, 20);
  624. dst_reg->valid = true;
  625. dst_modified = true;
  626. break;
  627. case HRE_LOADKEY:
  628. if (hre_op_loadkey(tpm, src_reg, dst_reg, data, data_size))
  629. return NULL;
  630. break;
  631. default:
  632. return NULL;
  633. }
  634. if (dst_reg && dst_modified && IS_PCR_HREG(dst_spec)) {
  635. hre_tpm_err = tpm1_extend(tpm, HREG_IDX(dst_spec),
  636. dst_reg->digest, dst_reg->digest);
  637. if (hre_tpm_err) {
  638. hre_err = HRE_E_TPM_FAILURE;
  639. return NULL;
  640. }
  641. }
  642. end:
  643. *ip += 4;
  644. *code_size -= 4;
  645. if (opcode & 0x80) {
  646. *ip += data_size;
  647. *code_size -= data_size;
  648. }
  649. return *ip;
  650. }
  651. /**
  652. * @brief runs a program on the hash register engine.
  653. * @param code pointer to the (HRE) code.
  654. * @param code_size size of the code (in bytes).
  655. * @return 0 on success, != 0 on failure.
  656. */
  657. static int hre_run_program(struct udevice *tpm, const uint8_t *code,
  658. size_t code_size)
  659. {
  660. size_t code_left;
  661. const uint8_t *ip = code;
  662. code_left = code_size;
  663. hre_tpm_err = 0;
  664. hre_err = HRE_E_OK;
  665. while (code_left > 0)
  666. if (!hre_execute_op(tpm, &ip, &code_left))
  667. return -1;
  668. return hre_err;
  669. }
  670. static int check_hmac(struct key_program *hmac,
  671. const uint8_t *data, size_t data_size)
  672. {
  673. uint8_t key[20], computed_hmac[20];
  674. uint32_t type;
  675. type = get_unaligned_be32(hmac->code);
  676. if (type != 0)
  677. return 1;
  678. memset(key, 0, sizeof(key));
  679. compute_extend(key, pcr_hregs[1].digest, 20);
  680. compute_extend(key, pcr_hregs[2].digest, 20);
  681. compute_extend(key, pcr_hregs[3].digest, 20);
  682. compute_extend(key, pcr_hregs[4].digest, 20);
  683. sha1_hmac(key, sizeof(key), data, data_size, computed_hmac);
  684. return memcmp(computed_hmac, hmac->code + 4, 20);
  685. }
  686. static int verify_program(struct key_program *prg)
  687. {
  688. uint32_t crc;
  689. crc = crc32(0, prg->code, prg->code_size);
  690. if (crc != prg->code_crc) {
  691. printf("HRC crc mismatch: %08x != %08x\n",
  692. crc, prg->code_crc);
  693. return 1;
  694. }
  695. return 0;
  696. }
  697. #if defined(CCDM_FIRST_STAGE) || (defined CCDM_AUTO_FIRST_STAGE)
  698. static struct key_program *load_sd_key_program(void)
  699. {
  700. u32 code_len, code_offset;
  701. struct mmc *mmc;
  702. u8 buf[128];
  703. struct key_program *result = NULL, *hmac = NULL;
  704. struct key_program header;
  705. mmc = find_mmc_device(0);
  706. if (!mmc)
  707. return NULL;
  708. mmc_init(mmc);
  709. if (ccdm_mmc_read(mmc, 0, buf, sizeof(buf)) <= 0)
  710. goto failure;
  711. code_offset = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ADDR_OFS);
  712. code_len = *(u32 *)(buf + ESDHC_BOOT_IMAGE_SIZE_OFS);
  713. code_offset += code_len;
  714. /* TODO: the following needs to be the size of the 2nd stage env */
  715. code_offset += CONFIG_ENV_SIZE;
  716. if (ccdm_mmc_read(mmc, code_offset, buf, 4*3) < 0)
  717. goto failure;
  718. header.magic = get_unaligned_be32(buf);
  719. header.code_crc = get_unaligned_be32(buf + 4);
  720. header.code_size = get_unaligned_be32(buf + 8);
  721. if (header.magic != MAGIC_KEY_PROGRAM)
  722. goto failure;
  723. result = malloc(sizeof(struct key_program) + header.code_size);
  724. if (!result)
  725. goto failure;
  726. *result = header;
  727. printf("load key program chunk from SD card (%u bytes) ",
  728. header.code_size);
  729. code_offset += 12;
  730. if (ccdm_mmc_read(mmc, code_offset, result->code, header.code_size)
  731. < 0)
  732. goto failure;
  733. code_offset += header.code_size;
  734. puts("\n");
  735. if (verify_program(result))
  736. goto failure;
  737. if (ccdm_mmc_read(mmc, code_offset, buf, 4*3) < 0)
  738. goto failure;
  739. header.magic = get_unaligned_be32(buf);
  740. header.code_crc = get_unaligned_be32(buf + 4);
  741. header.code_size = get_unaligned_be32(buf + 8);
  742. if (header.magic == MAGIC_HMAC) {
  743. puts("check integrity\n");
  744. hmac = malloc(sizeof(struct key_program) + header.code_size);
  745. if (!hmac)
  746. goto failure;
  747. *hmac = header;
  748. code_offset += 12;
  749. if (ccdm_mmc_read(mmc, code_offset, hmac->code,
  750. hmac->code_size) < 0)
  751. goto failure;
  752. if (verify_program(hmac))
  753. goto failure;
  754. if (check_hmac(hmac, result->code, result->code_size)) {
  755. puts("key program integrity could not be verified\n");
  756. goto failure;
  757. }
  758. puts("key program verified\n");
  759. }
  760. goto end;
  761. failure:
  762. if (result)
  763. free(result);
  764. result = NULL;
  765. end:
  766. if (hmac)
  767. free(hmac);
  768. return result;
  769. }
  770. #endif
  771. #ifdef CCDM_SECOND_STAGE
  772. /**
  773. * @brief load a key program from file system.
  774. * @param ifname interface of the file system
  775. * @param dev_part_str device part of the file system
  776. * @param fs_type tyep of the file system
  777. * @param path path of the file to load.
  778. * @return the loaded structure or NULL on failure.
  779. */
  780. static struct key_program *load_key_chunk(const char *ifname,
  781. const char *dev_part_str, int fs_type,
  782. const char *path)
  783. {
  784. struct key_program *result = NULL;
  785. struct key_program header;
  786. uint32_t crc;
  787. uint8_t buf[12];
  788. loff_t i;
  789. if (fs_set_blk_dev(ifname, dev_part_str, fs_type))
  790. goto failure;
  791. if (fs_read(path, (ulong)buf, 0, 12, &i) < 0)
  792. goto failure;
  793. if (i < 12)
  794. goto failure;
  795. header.magic = get_unaligned_be32(buf);
  796. header.code_crc = get_unaligned_be32(buf + 4);
  797. header.code_size = get_unaligned_be32(buf + 8);
  798. if (header.magic != MAGIC_HMAC && header.magic != MAGIC_KEY_PROGRAM)
  799. goto failure;
  800. result = malloc(sizeof(struct key_program) + header.code_size);
  801. if (!result)
  802. goto failure;
  803. if (fs_set_blk_dev(ifname, dev_part_str, fs_type))
  804. goto failure;
  805. if (fs_read(path, (ulong)result, 0,
  806. sizeof(struct key_program) + header.code_size, &i) < 0)
  807. goto failure;
  808. if (i <= 0)
  809. goto failure;
  810. *result = header;
  811. crc = crc32(0, result->code, result->code_size);
  812. if (crc != result->code_crc) {
  813. printf("%s: HRC crc mismatch: %08x != %08x\n",
  814. path, crc, result->code_crc);
  815. goto failure;
  816. }
  817. goto end;
  818. failure:
  819. if (result) {
  820. free(result);
  821. result = NULL;
  822. }
  823. end:
  824. return result;
  825. }
  826. #endif
  827. #if defined(CCDM_FIRST_STAGE) || (defined CCDM_AUTO_FIRST_STAGE)
  828. static const uint8_t prg_stage1_prepare[] = {
  829. 0x00, 0x20, 0x00, 0x00, /* opcode: SYNC f0 */
  830. 0x00, 0x24, 0x00, 0x00, /* opcode: SYNC f1 */
  831. 0x01, 0x80, 0x00, 0x00, /* opcode: CHECK0 PCR0 */
  832. 0x81, 0x22, 0x00, 0x00, /* opcode: LOAD PCR0, f0 */
  833. 0x01, 0x84, 0x00, 0x00, /* opcode: CHECK0 PCR1 */
  834. 0x81, 0x26, 0x10, 0x00, /* opcode: LOAD PCR1, f1 */
  835. 0x01, 0x88, 0x00, 0x00, /* opcode: CHECK0 PCR2 */
  836. 0x81, 0x2a, 0x20, 0x00, /* opcode: LOAD PCR2, f2 */
  837. 0x01, 0x8c, 0x00, 0x00, /* opcode: CHECK0 PCR3 */
  838. 0x81, 0x2e, 0x30, 0x00, /* opcode: LOAD PCR3, f3 */
  839. };
  840. static int first_stage_actions(struct udevice *tpm)
  841. {
  842. int result = 0;
  843. struct key_program *sd_prg = NULL;
  844. puts("CCDM S1: start actions\n");
  845. #ifndef CCDM_SECOND_STAGE
  846. if (tpm1_continue_self_test(tpm))
  847. goto failure;
  848. #else
  849. tpm1_continue_self_test(tpm);
  850. #endif
  851. mdelay(37);
  852. if (hre_run_program(tpm, prg_stage1_prepare,
  853. sizeof(prg_stage1_prepare)))
  854. goto failure;
  855. sd_prg = load_sd_key_program();
  856. if (sd_prg) {
  857. if (hre_run_program(tpm, sd_prg->code, sd_prg->code_size))
  858. goto failure;
  859. puts("SD code run successfully\n");
  860. } else {
  861. puts("no key program found on SD\n");
  862. goto failure;
  863. }
  864. goto end;
  865. failure:
  866. result = 1;
  867. end:
  868. if (sd_prg)
  869. free(sd_prg);
  870. printf("CCDM S1: actions done (%d)\n", result);
  871. return result;
  872. }
  873. #endif
  874. #ifdef CCDM_FIRST_STAGE
  875. static int first_stage_init(void)
  876. {
  877. struct udevice *tpm;
  878. int ret;
  879. puts("CCDM S1\n");
  880. ret = get_tpm(&tpm);
  881. if (ret || tpm_init(tpm) || tpm1_startup(tpm, TPM_ST_CLEAR))
  882. return 1;
  883. ret = first_stage_actions(tpm);
  884. #ifndef CCDM_SECOND_STAGE
  885. if (!ret) {
  886. if (bl2_entry)
  887. (*bl2_entry)();
  888. ret = 1;
  889. }
  890. #endif
  891. return ret;
  892. }
  893. #endif
  894. #ifdef CCDM_SECOND_STAGE
  895. static const uint8_t prg_stage2_prepare[] = {
  896. 0x00, 0x80, 0x00, 0x00, /* opcode: SYNC PCR0 */
  897. 0x00, 0x84, 0x00, 0x00, /* opcode: SYNC PCR1 */
  898. 0x00, 0x88, 0x00, 0x00, /* opcode: SYNC PCR2 */
  899. 0x00, 0x8c, 0x00, 0x00, /* opcode: SYNC PCR3 */
  900. 0x00, 0x90, 0x00, 0x00, /* opcode: SYNC PCR4 */
  901. };
  902. static const uint8_t prg_stage2_success[] = {
  903. 0x81, 0x02, 0x40, 0x14, /* opcode: LOAD PCR4, #<20B data> */
  904. 0x48, 0xfd, 0x95, 0x17, 0xe7, 0x54, 0x6b, 0x68, /* data */
  905. 0x92, 0x31, 0x18, 0x05, 0xf8, 0x58, 0x58, 0x3c, /* data */
  906. 0xe4, 0xd2, 0x81, 0xe0, /* data */
  907. };
  908. static const uint8_t prg_stage_fail[] = {
  909. 0x81, 0x01, 0x00, 0x14, /* opcode: LOAD v0, #<20B data> */
  910. 0xc0, 0x32, 0xad, 0xc1, 0xff, 0x62, 0x9c, 0x9b, /* data */
  911. 0x66, 0xf2, 0x27, 0x49, 0xad, 0x66, 0x7e, 0x6b, /* data */
  912. 0xea, 0xdf, 0x14, 0x4b, /* data */
  913. 0x81, 0x42, 0x30, 0x00, /* opcode: LOAD PCR3, v0 */
  914. 0x81, 0x42, 0x40, 0x00, /* opcode: LOAD PCR4, v0 */
  915. };
  916. static int second_stage_init(void)
  917. {
  918. static const char mac_suffix[] = ".mac";
  919. bool did_first_stage_run = true;
  920. int result = 0;
  921. char *cptr, *mmcdev = NULL;
  922. struct key_program *hmac_blob = NULL;
  923. const char *image_path = "/ccdm.itb";
  924. char *mac_path = NULL;
  925. ulong image_addr;
  926. loff_t image_size;
  927. struct udevice *tpm;
  928. uint32_t err;
  929. int ret;
  930. printf("CCDM S2\n");
  931. ret = get_tpm(&tpm);
  932. if (ret || tpm_init(tpm))
  933. return 1;
  934. err = tpm1_startup(tpm, TPM_ST_CLEAR);
  935. if (err != TPM_INVALID_POSTINIT)
  936. did_first_stage_run = false;
  937. #ifdef CCDM_AUTO_FIRST_STAGE
  938. if (!did_first_stage_run && first_stage_actions(tpm))
  939. goto failure;
  940. #else
  941. if (!did_first_stage_run)
  942. goto failure;
  943. #endif
  944. if (hre_run_program(tpm, prg_stage2_prepare,
  945. sizeof(prg_stage2_prepare)))
  946. goto failure;
  947. /* run "prepboot" from env to get "mmcdev" set */
  948. cptr = env_get("prepboot");
  949. if (cptr && !run_command(cptr, 0))
  950. mmcdev = env_get("mmcdev");
  951. if (!mmcdev)
  952. goto failure;
  953. cptr = env_get("ramdiskimage");
  954. if (cptr)
  955. image_path = cptr;
  956. mac_path = malloc(strlen(image_path) + strlen(mac_suffix) + 1);
  957. if (mac_path == NULL)
  958. goto failure;
  959. strcpy(mac_path, image_path);
  960. strcat(mac_path, mac_suffix);
  961. /* read image from mmcdev (ccdm.itb) */
  962. image_addr = (ulong)get_image_location();
  963. if (fs_set_blk_dev("mmc", mmcdev, FS_TYPE_EXT))
  964. goto failure;
  965. if (fs_read(image_path, image_addr, 0, 0, &image_size) < 0)
  966. goto failure;
  967. if (image_size <= 0)
  968. goto failure;
  969. printf("CCDM image found on %s, %lld bytes\n", mmcdev, image_size);
  970. hmac_blob = load_key_chunk("mmc", mmcdev, FS_TYPE_EXT, mac_path);
  971. if (!hmac_blob) {
  972. puts("failed to load mac file\n");
  973. goto failure;
  974. }
  975. if (verify_program(hmac_blob)) {
  976. puts("corrupted mac file\n");
  977. goto failure;
  978. }
  979. if (check_hmac(hmac_blob, (u8 *)image_addr, image_size)) {
  980. puts("image integrity could not be verified\n");
  981. goto failure;
  982. }
  983. puts("CCDM image OK\n");
  984. hre_run_program(tpm, prg_stage2_success, sizeof(prg_stage2_success));
  985. goto end;
  986. failure:
  987. result = 1;
  988. hre_run_program(tpm, prg_stage_fail, sizeof(prg_stage_fail));
  989. end:
  990. if (hmac_blob)
  991. free(hmac_blob);
  992. if (mac_path)
  993. free(mac_path);
  994. return result;
  995. }
  996. #endif
  997. int show_self_hash(void)
  998. {
  999. struct h_reg *hash_ptr;
  1000. #ifdef CCDM_SECOND_STAGE
  1001. struct h_reg hash;
  1002. hash_ptr = &hash;
  1003. if (compute_self_hash(hash_ptr))
  1004. return 1;
  1005. #else
  1006. hash_ptr = &fix_hregs[FIX_HREG_SELF_HASH];
  1007. #endif
  1008. puts("self hash: ");
  1009. if (hash_ptr && hash_ptr->valid)
  1010. print_buffer(0, hash_ptr->digest, 1, 20, 20);
  1011. else
  1012. puts("INVALID\n");
  1013. return 0;
  1014. }
  1015. /**
  1016. * @brief let the system hang.
  1017. *
  1018. * Called on error.
  1019. * Will stop the boot process; display a message and signal the error condition
  1020. * by blinking the "status" and the "finder" LED of the controller board.
  1021. *
  1022. * @note the develop version runs the blink cycle 2 times and then returns.
  1023. * The release version never returns.
  1024. */
  1025. static void ccdm_hang(void)
  1026. {
  1027. static const u64 f0 = 0x0ba3bb8ba2e880; /* blink code "finder" LED */
  1028. static const u64 s0 = 0x00f0f0f0f0f0f0; /* blink code "status" LED */
  1029. u64 f, s;
  1030. int i;
  1031. #ifdef CCDM_DEVELOP
  1032. int j;
  1033. #endif
  1034. I2C_SET_BUS(I2C_SOC_0);
  1035. pca9698_direction_output(0x22, 0, 0); /* Finder */
  1036. pca9698_direction_output(0x22, 4, 0); /* Status */
  1037. puts("### ERROR ### Please RESET the board ###\n");
  1038. bootstage_error(BOOTSTAGE_ID_NEED_RESET);
  1039. #ifdef CCDM_DEVELOP
  1040. puts("*** ERROR ******** THIS WOULD HANG ******** ERROR ***\n");
  1041. puts("** but we continue since this is a DEVELOP version **\n");
  1042. puts("*** ERROR ******** THIS WOULD HANG ******** ERROR ***\n");
  1043. for (j = 2; j-- > 0;) {
  1044. putc('#');
  1045. #else
  1046. for (;;) {
  1047. #endif
  1048. f = f0;
  1049. s = s0;
  1050. for (i = 54; i-- > 0;) {
  1051. pca9698_set_value(0x22, 0, !(f & 1));
  1052. pca9698_set_value(0x22, 4, (s & 1));
  1053. f >>= 1;
  1054. s >>= 1;
  1055. mdelay(120);
  1056. }
  1057. }
  1058. puts("\ncontinue...\n");
  1059. }
  1060. int startup_ccdm_id_module(void)
  1061. {
  1062. int result = 0;
  1063. unsigned int orig_i2c_bus;
  1064. orig_i2c_bus = i2c_get_bus_num();
  1065. i2c_set_bus_num(I2C_SOC_1);
  1066. /* goto end; */
  1067. #ifdef CCDM_DEVELOP
  1068. show_self_hash();
  1069. #endif
  1070. #ifdef CCDM_FIRST_STAGE
  1071. result = first_stage_init();
  1072. if (result) {
  1073. puts("1st stage init failed\n");
  1074. goto failure;
  1075. }
  1076. #endif
  1077. #ifdef CCDM_SECOND_STAGE
  1078. result = second_stage_init();
  1079. if (result) {
  1080. puts("2nd stage init failed\n");
  1081. goto failure;
  1082. }
  1083. #endif
  1084. goto end;
  1085. failure:
  1086. result = 1;
  1087. end:
  1088. i2c_set_bus_num(orig_i2c_bus);
  1089. if (result)
  1090. ccdm_hang();
  1091. return result;
  1092. }