controlcenterd-id.c 28 KB

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