controlcenterd-id.c 28 KB

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