controlcenterd-id.c 28 KB

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