controlcenterd-id.c 28 KB

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