controlcenterd-id.c 28 KB

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