controlcenterd-id.c 28 KB

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