controlcenterd-id.c 28 KB

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