controlcenterd-id.c 28 KB

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