blob: ad56dd7d019d98369adf6ce2eca061b21d78cda3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Dirk Eibach762d3df2013-06-26 15:55:17 +02002/*
3 * (C) Copyright 2013
4 * Reinhard Pfau, Guntermann & Drunck GmbH, reinhard.pfau@gdsys.cc
Dirk Eibach762d3df2013-06-26 15:55:17 +02005 */
6
7/* TODO: some more #ifdef's to avoid unneeded code for stage 1 / stage 2 */
8
9#ifdef CCDM_ID_DEBUG
10#define DEBUG
11#endif
12
13#include <common.h>
Simon Glassadaaa482019-11-14 12:57:43 -070014#include <command.h>
Simon Glass8ceca1d2018-11-18 14:22:27 -070015#include <dm.h>
Simon Glass6eaea252019-08-01 09:46:48 -060016#include <env.h>
Simon Glassf11478f2019-12-28 10:45:07 -070017#include <hang.h>
Dirk Eibach762d3df2013-06-26 15:55:17 +020018#include <malloc.h>
19#include <fs.h>
20#include <i2c.h>
21#include <mmc.h>
Miquel Raynal4c6759e2018-05-15 11:57:06 +020022#include <tpm-v1.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070023#include <u-boot/crc.h>
Jeroen Hofsteebfe88fe2014-06-12 22:27:12 +020024#include <u-boot/sha1.h>
Dirk Eibach762d3df2013-06-26 15:55:17 +020025#include <asm/byteorder.h>
26#include <asm/unaligned.h>
27#include <pca9698.h>
28
29#undef CCDM_FIRST_STAGE
30#undef CCDM_SECOND_STAGE
31#undef CCDM_AUTO_FIRST_STAGE
32
33#ifdef CONFIG_DEVELOP
34#define CCDM_DEVELOP
35#endif
36
37#ifdef CONFIG_TRAILBLAZER
38#define CCDM_FIRST_STAGE
39#undef CCDM_SECOND_STAGE
40#else
41#undef CCDM_FIRST_STAGE
42#define CCDM_SECOND_STAGE
43#endif
44
45#if defined(CCDM_DEVELOP) && defined(CCDM_SECOND_STAGE) && \
46 !defined(CCCM_FIRST_STAGE)
47#define CCDM_AUTO_FIRST_STAGE
48#endif
49
Dirk Eibach762d3df2013-06-26 15:55:17 +020050/* CCDM specific contants */
51enum {
52 /* NV indices */
53 NV_COMMON_DATA_INDEX = 0x40000001,
54 /* magics for key blob chains */
55 MAGIC_KEY_PROGRAM = 0x68726500,
56 MAGIC_HMAC = 0x68616300,
57 MAGIC_END_OF_CHAIN = 0x00000000,
58 /* sizes */
59 NV_COMMON_DATA_MIN_SIZE = 3 * sizeof(uint64_t) + 2 * sizeof(uint16_t),
60};
61
62/* other constants */
63enum {
64 ESDHC_BOOT_IMAGE_SIG_OFS = 0x40,
65 ESDHC_BOOT_IMAGE_SIZE_OFS = 0x48,
66 ESDHC_BOOT_IMAGE_ADDR_OFS = 0x50,
67 ESDHC_BOOT_IMAGE_TARGET_OFS = 0x58,
68 ESDHC_BOOT_IMAGE_ENTRY_OFS = 0x60,
69};
70
Dirk Eibache674bf12014-07-03 09:28:16 +020071enum {
72 I2C_SOC_0 = 0,
73 I2C_SOC_1 = 1,
74};
75
Dirk Eibach762d3df2013-06-26 15:55:17 +020076struct key_program {
77 uint32_t magic;
78 uint32_t code_crc;
79 uint32_t code_size;
80 uint8_t code[];
81};
82
83struct h_reg {
84 bool valid;
85 uint8_t digest[20];
86};
87
88
89enum access_mode {
90 HREG_NONE = 0,
91 HREG_RD = 1,
92 HREG_WR = 2,
93 HREG_RDWR = 3,
94};
95
96/* register constants */
97enum {
98 FIX_HREG_DEVICE_ID_HASH = 0,
99 FIX_HREG_SELF_HASH = 1,
100 FIX_HREG_STAGE2_HASH = 2,
101 FIX_HREG_VENDOR = 3,
102 COUNT_FIX_HREGS
103};
104
105
106/* hre opcodes */
107enum {
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
123/* hre errors */
124enum {
125 HRE_E_OK = 0,
126 HRE_E_TPM_FAILURE,
127 HRE_E_INVALID_HREG,
128};
129
130static uint64_t device_id;
131static uint64_t device_cl;
132static uint64_t device_type;
133
134static uint32_t platform_key_handle;
135
136static void(*bl2_entry)(void);
137
138static struct h_reg pcr_hregs[24];
139static struct h_reg fix_hregs[COUNT_FIX_HREGS];
140static struct h_reg var_hregs[8];
141static uint32_t hre_tpm_err;
142static int hre_err = HRE_E_OK;
143
144#define IS_PCR_HREG(spec) ((spec) & 0x20)
145#define IS_FIX_HREG(spec) (((spec) & 0x38) == 0x08)
146#define IS_VAR_HREG(spec) (((spec) & 0x38) == 0x10)
147#define HREG_IDX(spec) ((spec) & (IS_PCR_HREG(spec) ? 0x1f : 0x7))
148
Simon Glass8ceca1d2018-11-18 14:22:27 -0700149static int get_tpm(struct udevice **devp)
150{
151 int rc;
152
153 rc = uclass_first_device_err(UCLASS_TPM, devp);
154 if (rc) {
155 printf("Could not find TPM (ret=%d)\n", rc);
156 return CMD_RET_FAILURE;
157 }
158
159 return 0;
160}
161
Dirk Eibach762d3df2013-06-26 15:55:17 +0200162static const uint8_t vendor[] = "Guntermann & Drunck";
163
Dirk Eibach762d3df2013-06-26 15:55:17 +0200164/**
165 * @brief read a bunch of data from MMC into memory.
166 *
167 * @param mmc pointer to the mmc structure to use.
168 * @param src offset where the data starts on MMC/SD device (in bytes).
169 * @param dst pointer to the location where the read data should be stored.
170 * @param size number of bytes to read from the MMC/SD device.
171 * @return number of bytes read or -1 on error.
172 */
173static int ccdm_mmc_read(struct mmc *mmc, u64 src, u8 *dst, int size)
174{
175 int result = 0;
176 u32 blk_len, ofs;
177 ulong block_no, n, cnt;
178 u8 *tmp_buf = NULL;
179
180 if (size <= 0)
181 goto end;
182
183 blk_len = mmc->read_bl_len;
184 tmp_buf = malloc(blk_len);
185 if (!tmp_buf)
186 goto failure;
187 block_no = src / blk_len;
188 ofs = src % blk_len;
189
190 if (ofs) {
Stephen Warrene73f2962015-12-07 11:38:48 -0700191 n = mmc->block_dev.block_read(&mmc->block_dev, block_no++, 1,
Dirk Eibach762d3df2013-06-26 15:55:17 +0200192 tmp_buf);
193 if (!n)
194 goto failure;
Masahiro Yamadadb204642014-11-07 03:03:31 +0900195 result = min(size, (int)(blk_len - ofs));
Dirk Eibach762d3df2013-06-26 15:55:17 +0200196 memcpy(dst, tmp_buf + ofs, result);
197 dst += result;
198 size -= result;
199 }
200 cnt = size / blk_len;
201 if (cnt) {
Stephen Warrene73f2962015-12-07 11:38:48 -0700202 n = mmc->block_dev.block_read(&mmc->block_dev, block_no, cnt,
Dirk Eibach762d3df2013-06-26 15:55:17 +0200203 dst);
204 if (n != cnt)
205 goto failure;
206 size -= cnt * blk_len;
207 result += cnt * blk_len;
208 dst += cnt * blk_len;
209 block_no += cnt;
210 }
211 if (size) {
Stephen Warrene73f2962015-12-07 11:38:48 -0700212 n = mmc->block_dev.block_read(&mmc->block_dev, block_no++, 1,
Dirk Eibach762d3df2013-06-26 15:55:17 +0200213 tmp_buf);
214 if (!n)
215 goto failure;
216 memcpy(dst, tmp_buf, size);
217 result += size;
218 }
219 goto end;
220failure:
221 result = -1;
222end:
223 if (tmp_buf)
224 free(tmp_buf);
225 return result;
226}
227
228/**
229 * @brief returns a location where the 2nd stage bootloader can be(/ is) placed.
230 *
231 * @return pointer to the location for/of the 2nd stage bootloader
232 */
233static u8 *get_2nd_stage_bl_location(ulong target_addr)
234{
235 ulong addr;
236#ifdef CCDM_SECOND_STAGE
Simon Glass22c34c22017-08-03 12:22:13 -0600237 addr = env_get_ulong("loadaddr", 16, CONFIG_LOADADDR);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200238#else
239 addr = target_addr;
240#endif
241 return (u8 *)(addr);
242}
243
244
245#ifdef CCDM_SECOND_STAGE
246/**
247 * @brief returns a location where the image can be(/ is) placed.
248 *
249 * @return pointer to the location for/of the image
250 */
251static u8 *get_image_location(void)
252{
253 ulong addr;
254 /* TODO use other area? */
Simon Glass22c34c22017-08-03 12:22:13 -0600255 addr = env_get_ulong("loadaddr", 16, CONFIG_LOADADDR);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200256 return (u8 *)(addr);
257}
258#endif
259
260/**
261 * @brief get the size of a given (TPM) NV area
262 * @param index NV index of the area to get size for
263 * @param size pointer to the size
264 * @return 0 on success, != 0 on error
265 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700266static int get_tpm_nv_size(struct udevice *tpm, uint32_t index, uint32_t *size)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200267{
268 uint32_t err;
269 uint8_t info[72];
270 uint8_t *ptr;
271 uint16_t v16;
272
Simon Glass8ceca1d2018-11-18 14:22:27 -0700273 err = tpm_get_capability(tpm, TPM_CAP_NV_INDEX, index,
274 info, sizeof(info));
Dirk Eibach762d3df2013-06-26 15:55:17 +0200275 if (err) {
276 printf("tpm_get_capability(CAP_NV_INDEX, %08x) failed: %u\n",
277 index, err);
278 return 1;
279 }
280
281 /* skip tag and nvIndex */
282 ptr = info + 6;
283 /* skip 2 pcr info fields */
284 v16 = get_unaligned_be16(ptr);
285 ptr += 2 + v16 + 1 + 20;
286 v16 = get_unaligned_be16(ptr);
287 ptr += 2 + v16 + 1 + 20;
288 /* skip permission and flags */
289 ptr += 6 + 3;
290
291 *size = get_unaligned_be32(ptr);
292 return 0;
293}
294
295/**
296 * @brief search for a key by usage auth and pub key hash.
297 * @param auth usage auth of the key to search for
298 * @param pubkey_digest (SHA1) hash of the pub key structure of the key
299 * @param[out] handle the handle of the key iff found
300 * @return 0 if key was found in TPM; != 0 if not.
301 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700302static int find_key(struct udevice *tpm, const uint8_t auth[20],
303 const uint8_t pubkey_digest[20], uint32_t *handle)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200304{
305 uint16_t key_count;
306 uint32_t key_handles[10];
307 uint8_t buf[288];
308 uint8_t *ptr;
309 uint32_t err;
310 uint8_t digest[20];
311 size_t buf_len;
312 unsigned int i;
313
314 /* fetch list of already loaded keys in the TPM */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700315 err = tpm_get_capability(tpm, TPM_CAP_HANDLE, TPM_RT_KEY, buf,
316 sizeof(buf));
Dirk Eibach762d3df2013-06-26 15:55:17 +0200317 if (err)
318 return -1;
319 key_count = get_unaligned_be16(buf);
320 ptr = buf + 2;
321 for (i = 0; i < key_count; ++i, ptr += 4)
322 key_handles[i] = get_unaligned_be32(ptr);
323
324 /* now search a(/ the) key which we can access with the given auth */
325 for (i = 0; i < key_count; ++i) {
326 buf_len = sizeof(buf);
Simon Glass8ceca1d2018-11-18 14:22:27 -0700327 err = tpm_get_pub_key_oiap(tpm, key_handles[i], auth, buf,
328 &buf_len);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200329 if (err && err != TPM_AUTHFAIL)
330 return -1;
331 if (err)
332 continue;
333 sha1_csum(buf, buf_len, digest);
334 if (!memcmp(digest, pubkey_digest, 20)) {
335 *handle = key_handles[i];
336 return 0;
337 }
338 }
339 return 1;
340}
341
342/**
343 * @brief read CCDM common data from TPM NV
344 * @return 0 if CCDM common data was found and read, !=0 if something failed.
345 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700346static int read_common_data(struct udevice *tpm)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200347{
348 uint32_t size;
349 uint32_t err;
350 uint8_t buf[256];
351 sha1_context ctx;
352
Simon Glass8ceca1d2018-11-18 14:22:27 -0700353 if (get_tpm_nv_size(tpm, NV_COMMON_DATA_INDEX, &size) ||
Dirk Eibach762d3df2013-06-26 15:55:17 +0200354 size < NV_COMMON_DATA_MIN_SIZE)
355 return 1;
Simon Glass8ceca1d2018-11-18 14:22:27 -0700356 err = tpm_nv_read_value(tpm, NV_COMMON_DATA_INDEX,
357 buf, min(sizeof(buf), size));
Dirk Eibach762d3df2013-06-26 15:55:17 +0200358 if (err) {
359 printf("tpm_nv_read_value() failed: %u\n", err);
360 return 1;
361 }
362
363 device_id = get_unaligned_be64(buf);
364 device_cl = get_unaligned_be64(buf + 8);
365 device_type = get_unaligned_be64(buf + 16);
366
367 sha1_starts(&ctx);
368 sha1_update(&ctx, buf, 24);
369 sha1_finish(&ctx, fix_hregs[FIX_HREG_DEVICE_ID_HASH].digest);
370 fix_hregs[FIX_HREG_DEVICE_ID_HASH].valid = true;
371
372 platform_key_handle = get_unaligned_be32(buf + 24);
373
374 return 0;
375}
376
377/**
378 * @brief compute hash of bootloader itself.
379 * @param[out] dst hash register where the hash should be stored
380 * @return 0 on success, != 0 on failure.
381 *
382 * @note MUST be called at a time where the boot loader is accessible at the
383 * configured location (; so take care when code is reallocated).
384 */
385static int compute_self_hash(struct h_reg *dst)
386{
387 sha1_csum((const uint8_t *)CONFIG_SYS_MONITOR_BASE,
388 CONFIG_SYS_MONITOR_LEN, dst->digest);
389 dst->valid = true;
390 return 0;
391}
392
393int ccdm_compute_self_hash(void)
394{
395 if (!fix_hregs[FIX_HREG_SELF_HASH].valid)
396 compute_self_hash(&fix_hregs[FIX_HREG_SELF_HASH]);
397 return 0;
398}
399
400/**
401 * @brief compute the hash of the 2nd stage boot loader (on SD card)
402 * @param[out] dst hash register to store the computed hash
403 * @return 0 on success, != 0 on failure
404 *
405 * Determines the size and location of the 2nd stage boot loader on SD card,
406 * loads the 2nd stage boot loader and computes the (SHA1) hash value.
407 * Within the 1st stage boot loader, the 2nd stage boot loader is loaded at
408 * the desired memory location and the variable @a bl2_entry is set.
409 *
410 * @note This sets the variable @a bl2_entry to the entry point when the
411 * 2nd stage boot loader is loaded at its configured memory location.
412 */
413static int compute_second_stage_hash(struct h_reg *dst)
414{
415 int result = 0;
416 u32 code_len, code_offset, target_addr, exec_entry;
417 struct mmc *mmc;
418 u8 *load_addr = NULL;
419 u8 buf[128];
420
421 mmc = find_mmc_device(0);
422 if (!mmc)
423 goto failure;
424 mmc_init(mmc);
425
426 if (ccdm_mmc_read(mmc, 0, buf, sizeof(buf)) < 0)
427 goto failure;
428
429 code_offset = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ADDR_OFS);
430 code_len = *(u32 *)(buf + ESDHC_BOOT_IMAGE_SIZE_OFS);
431 target_addr = *(u32 *)(buf + ESDHC_BOOT_IMAGE_TARGET_OFS);
432 exec_entry = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ENTRY_OFS);
433
434 load_addr = get_2nd_stage_bl_location(target_addr);
435 if (load_addr == (u8 *)target_addr)
436 bl2_entry = (void(*)(void))exec_entry;
437
438 if (ccdm_mmc_read(mmc, code_offset, load_addr, code_len) < 0)
439 goto failure;
440
441 sha1_csum(load_addr, code_len, dst->digest);
442 dst->valid = true;
443
444 goto end;
445failure:
446 result = 1;
447 bl2_entry = NULL;
448end:
449 return result;
450}
451
452/**
453 * @brief get pointer to hash register by specification
454 * @param spec specification of a hash register
455 * @return pointer to hash register or NULL if @a spec does not qualify a
456 * valid hash register; NULL else.
457 */
458static struct h_reg *get_hreg(uint8_t spec)
459{
460 uint8_t idx;
461
462 idx = HREG_IDX(spec);
463 if (IS_FIX_HREG(spec)) {
464 if (idx < ARRAY_SIZE(fix_hregs))
465 return fix_hregs + idx;
466 hre_err = HRE_E_INVALID_HREG;
467 } else if (IS_PCR_HREG(spec)) {
468 if (idx < ARRAY_SIZE(pcr_hregs))
469 return pcr_hregs + idx;
470 hre_err = HRE_E_INVALID_HREG;
471 } else if (IS_VAR_HREG(spec)) {
472 if (idx < ARRAY_SIZE(var_hregs))
473 return var_hregs + idx;
474 hre_err = HRE_E_INVALID_HREG;
475 }
476 return NULL;
477}
478
479/**
480 * @brief get pointer of a hash register by specification and usage.
481 * @param spec specification of a hash register
482 * @param mode access mode (read or write or read/write)
483 * @return pointer to hash register if found and valid; NULL else.
484 *
485 * This func uses @a get_reg() to determine the hash register for a given spec.
486 * If a register is found it is validated according to the desired access mode.
487 * The value of automatic registers (PCR register and fixed registers) is
488 * loaded or computed on read access.
489 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700490static struct h_reg *access_hreg(struct udevice *tpm, uint8_t spec,
491 enum access_mode mode)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200492{
493 struct h_reg *result;
494
495 result = get_hreg(spec);
496 if (!result)
497 return NULL;
498
499 if (mode & HREG_WR) {
500 if (IS_FIX_HREG(spec)) {
501 hre_err = HRE_E_INVALID_HREG;
502 return NULL;
503 }
504 }
505 if (mode & HREG_RD) {
506 if (!result->valid) {
507 if (IS_PCR_HREG(spec)) {
Simon Glass8ceca1d2018-11-18 14:22:27 -0700508 hre_tpm_err = tpm_pcr_read(tpm, HREG_IDX(spec),
Dirk Eibach762d3df2013-06-26 15:55:17 +0200509 result->digest, 20);
510 result->valid = (hre_tpm_err == TPM_SUCCESS);
511 } else if (IS_FIX_HREG(spec)) {
512 switch (HREG_IDX(spec)) {
513 case FIX_HREG_DEVICE_ID_HASH:
Simon Glass8ceca1d2018-11-18 14:22:27 -0700514 read_common_data(tpm);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200515 break;
516 case FIX_HREG_SELF_HASH:
517 ccdm_compute_self_hash();
518 break;
519 case FIX_HREG_STAGE2_HASH:
520 compute_second_stage_hash(result);
521 break;
522 case FIX_HREG_VENDOR:
523 memcpy(result->digest, vendor, 20);
524 result->valid = true;
525 break;
526 }
527 } else {
528 result->valid = true;
529 }
530 }
531 if (!result->valid) {
532 hre_err = HRE_E_INVALID_HREG;
533 return NULL;
534 }
535 }
536
537 return result;
538}
539
540static void *compute_and(void *_dst, const void *_src, size_t n)
541{
542 uint8_t *dst = _dst;
543 const uint8_t *src = _src;
544 size_t i;
545
546 for (i = n; i-- > 0; )
547 *dst++ &= *src++;
548
549 return _dst;
550}
551
552static void *compute_or(void *_dst, const void *_src, size_t n)
553{
554 uint8_t *dst = _dst;
555 const uint8_t *src = _src;
556 size_t i;
557
558 for (i = n; i-- > 0; )
559 *dst++ |= *src++;
560
561 return _dst;
562}
563
564static void *compute_xor(void *_dst, const void *_src, size_t n)
565{
566 uint8_t *dst = _dst;
567 const uint8_t *src = _src;
568 size_t i;
569
570 for (i = n; i-- > 0; )
571 *dst++ ^= *src++;
572
573 return _dst;
574}
575
576static void *compute_extend(void *_dst, const void *_src, size_t n)
577{
578 uint8_t digest[20];
579 sha1_context ctx;
580
581 sha1_starts(&ctx);
582 sha1_update(&ctx, _dst, n);
583 sha1_update(&ctx, _src, n);
584 sha1_finish(&ctx, digest);
585 memcpy(_dst, digest, min(n, sizeof(digest)));
586
587 return _dst;
588}
589
Simon Glass8ceca1d2018-11-18 14:22:27 -0700590static int hre_op_loadkey(struct udevice *tpm, struct h_reg *src_reg,
591 struct h_reg *dst_reg, const void *key,
592 size_t key_size)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200593{
594 uint32_t parent_handle;
595 uint32_t key_handle;
596
597 if (!src_reg || !dst_reg || !src_reg->valid || !dst_reg->valid)
598 return -1;
Simon Glass8ceca1d2018-11-18 14:22:27 -0700599 if (find_key(tpm, src_reg->digest, dst_reg->digest, &parent_handle))
Dirk Eibach762d3df2013-06-26 15:55:17 +0200600 return -1;
Simon Glass8ceca1d2018-11-18 14:22:27 -0700601 hre_tpm_err = tpm_load_key2_oiap(tpm, parent_handle, key, key_size,
602 src_reg->digest, &key_handle);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200603 if (hre_tpm_err) {
604 hre_err = HRE_E_TPM_FAILURE;
605 return -1;
606 }
607 /* TODO remember key handle somehow? */
608
609 return 0;
610}
611
612/**
613 * @brief executes the next opcode on the hash register engine.
614 * @param[in,out] ip pointer to the opcode (instruction pointer)
615 * @param[in,out] code_size (remaining) size of the code
616 * @return new instruction pointer on success, NULL on error.
617 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700618static const uint8_t *hre_execute_op(struct udevice *tpm, const uint8_t **ip,
619 size_t *code_size)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200620{
621 bool dst_modified = false;
622 uint32_t ins;
623 uint8_t opcode;
624 uint8_t src_spec;
625 uint8_t dst_spec;
626 uint16_t data_size;
627 struct h_reg *src_reg, *dst_reg;
628 uint8_t buf[20];
629 const uint8_t *src_buf, *data;
630 uint8_t *ptr;
631 int i;
632 void * (*bin_func)(void *, const void *, size_t);
633
634 if (*code_size < 4)
635 return NULL;
636
637 ins = get_unaligned_be32(*ip);
638 opcode = **ip;
639 data = *ip + 4;
640 src_spec = (ins >> 18) & 0x3f;
641 dst_spec = (ins >> 12) & 0x3f;
642 data_size = (ins & 0x7ff);
643
644 debug("HRE: ins=%08x (op=%02x, s=%02x, d=%02x, L=%d)\n", ins,
645 opcode, src_spec, dst_spec, data_size);
646
647 if ((opcode & 0x80) && (data_size + 4) > *code_size)
648 return NULL;
649
Simon Glass8ceca1d2018-11-18 14:22:27 -0700650 src_reg = access_hreg(tpm, src_spec, HREG_RD);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200651 if (hre_err || hre_tpm_err)
652 return NULL;
Simon Glass8ceca1d2018-11-18 14:22:27 -0700653 dst_reg = access_hreg(tpm, dst_spec,
654 (opcode & 0x40) ? HREG_RDWR : HREG_WR);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200655 if (hre_err || hre_tpm_err)
656 return NULL;
657
658 switch (opcode) {
659 case HRE_NOP:
660 goto end;
661 case HRE_CHECK0:
662 if (src_reg) {
663 for (i = 0; i < 20; ++i) {
664 if (src_reg->digest[i])
665 return NULL;
666 }
667 }
668 break;
669 case HRE_LOAD:
670 bin_func = memcpy;
671 goto do_bin_func;
672 case HRE_XOR:
673 bin_func = compute_xor;
674 goto do_bin_func;
675 case HRE_AND:
676 bin_func = compute_and;
677 goto do_bin_func;
678 case HRE_OR:
679 bin_func = compute_or;
680 goto do_bin_func;
681 case HRE_EXTEND:
682 bin_func = compute_extend;
683do_bin_func:
684 if (!dst_reg)
685 return NULL;
686 if (src_reg) {
687 src_buf = src_reg->digest;
688 } else {
689 if (!data_size) {
690 memset(buf, 0, 20);
691 src_buf = buf;
692 } else if (data_size == 1) {
693 memset(buf, *data, 20);
694 src_buf = buf;
695 } else if (data_size >= 20) {
696 src_buf = data;
697 } else {
698 src_buf = buf;
699 for (ptr = (uint8_t *)src_buf, i = 20; i > 0;
700 i -= data_size, ptr += data_size)
Masahiro Yamadadb204642014-11-07 03:03:31 +0900701 memcpy(ptr, data,
702 min_t(size_t, i, data_size));
Dirk Eibach762d3df2013-06-26 15:55:17 +0200703 }
704 }
705 bin_func(dst_reg->digest, src_buf, 20);
706 dst_reg->valid = true;
707 dst_modified = true;
708 break;
709 case HRE_LOADKEY:
Simon Glass8ceca1d2018-11-18 14:22:27 -0700710 if (hre_op_loadkey(tpm, src_reg, dst_reg, data, data_size))
Dirk Eibach762d3df2013-06-26 15:55:17 +0200711 return NULL;
712 break;
713 default:
714 return NULL;
715 }
716
717 if (dst_reg && dst_modified && IS_PCR_HREG(dst_spec)) {
Simon Glass8ceca1d2018-11-18 14:22:27 -0700718 hre_tpm_err = tpm_extend(tpm, HREG_IDX(dst_spec),
719 dst_reg->digest, dst_reg->digest);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200720 if (hre_tpm_err) {
721 hre_err = HRE_E_TPM_FAILURE;
722 return NULL;
723 }
724 }
725end:
726 *ip += 4;
727 *code_size -= 4;
728 if (opcode & 0x80) {
729 *ip += data_size;
730 *code_size -= data_size;
731 }
732
733 return *ip;
734}
735
736/**
737 * @brief runs a program on the hash register engine.
738 * @param code pointer to the (HRE) code.
739 * @param code_size size of the code (in bytes).
740 * @return 0 on success, != 0 on failure.
741 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700742static int hre_run_program(struct udevice *tpm, const uint8_t *code,
743 size_t code_size)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200744{
745 size_t code_left;
746 const uint8_t *ip = code;
747
748 code_left = code_size;
749 hre_tpm_err = 0;
750 hre_err = HRE_E_OK;
751 while (code_left > 0)
Simon Glass8ceca1d2018-11-18 14:22:27 -0700752 if (!hre_execute_op(tpm, &ip, &code_left))
Dirk Eibach762d3df2013-06-26 15:55:17 +0200753 return -1;
754
755 return hre_err;
756}
757
758static int check_hmac(struct key_program *hmac,
759 const uint8_t *data, size_t data_size)
760{
761 uint8_t key[20], computed_hmac[20];
762 uint32_t type;
763
764 type = get_unaligned_be32(hmac->code);
765 if (type != 0)
766 return 1;
767 memset(key, 0, sizeof(key));
768 compute_extend(key, pcr_hregs[1].digest, 20);
769 compute_extend(key, pcr_hregs[2].digest, 20);
770 compute_extend(key, pcr_hregs[3].digest, 20);
771 compute_extend(key, pcr_hregs[4].digest, 20);
772
773 sha1_hmac(key, sizeof(key), data, data_size, computed_hmac);
774
775 return memcmp(computed_hmac, hmac->code + 4, 20);
776}
777
778static int verify_program(struct key_program *prg)
779{
780 uint32_t crc;
781 crc = crc32(0, prg->code, prg->code_size);
782
783 if (crc != prg->code_crc) {
784 printf("HRC crc mismatch: %08x != %08x\n",
785 crc, prg->code_crc);
786 return 1;
787 }
788 return 0;
789}
790
791#if defined(CCDM_FIRST_STAGE) || (defined CCDM_AUTO_FIRST_STAGE)
792static struct key_program *load_sd_key_program(void)
793{
794 u32 code_len, code_offset;
795 struct mmc *mmc;
796 u8 buf[128];
797 struct key_program *result = NULL, *hmac = NULL;
798 struct key_program header;
799
800 mmc = find_mmc_device(0);
801 if (!mmc)
802 return NULL;
803 mmc_init(mmc);
804
805 if (ccdm_mmc_read(mmc, 0, buf, sizeof(buf)) <= 0)
806 goto failure;
807
808 code_offset = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ADDR_OFS);
809 code_len = *(u32 *)(buf + ESDHC_BOOT_IMAGE_SIZE_OFS);
810
811 code_offset += code_len;
812 /* TODO: the following needs to be the size of the 2nd stage env */
813 code_offset += CONFIG_ENV_SIZE;
814
815 if (ccdm_mmc_read(mmc, code_offset, buf, 4*3) < 0)
816 goto failure;
817
818 header.magic = get_unaligned_be32(buf);
819 header.code_crc = get_unaligned_be32(buf + 4);
820 header.code_size = get_unaligned_be32(buf + 8);
821
822 if (header.magic != MAGIC_KEY_PROGRAM)
823 goto failure;
824
825 result = malloc(sizeof(struct key_program) + header.code_size);
826 if (!result)
827 goto failure;
828 *result = header;
829
830 printf("load key program chunk from SD card (%u bytes) ",
831 header.code_size);
832 code_offset += 12;
833 if (ccdm_mmc_read(mmc, code_offset, result->code, header.code_size)
834 < 0)
835 goto failure;
836 code_offset += header.code_size;
837 puts("\n");
838
839 if (verify_program(result))
840 goto failure;
841
842 if (ccdm_mmc_read(mmc, code_offset, buf, 4*3) < 0)
843 goto failure;
844
845 header.magic = get_unaligned_be32(buf);
846 header.code_crc = get_unaligned_be32(buf + 4);
847 header.code_size = get_unaligned_be32(buf + 8);
848
849 if (header.magic == MAGIC_HMAC) {
850 puts("check integrity\n");
851 hmac = malloc(sizeof(struct key_program) + header.code_size);
852 if (!hmac)
853 goto failure;
854 *hmac = header;
855 code_offset += 12;
856 if (ccdm_mmc_read(mmc, code_offset, hmac->code,
857 hmac->code_size) < 0)
858 goto failure;
859 if (verify_program(hmac))
860 goto failure;
861 if (check_hmac(hmac, result->code, result->code_size)) {
862 puts("key program integrity could not be verified\n");
863 goto failure;
864 }
865 puts("key program verified\n");
866 }
867
868 goto end;
869failure:
870 if (result)
871 free(result);
872 result = NULL;
873end:
874 if (hmac)
875 free(hmac);
876
877 return result;
878}
879#endif
880
881#ifdef CCDM_SECOND_STAGE
882/**
883 * @brief load a key program from file system.
884 * @param ifname interface of the file system
885 * @param dev_part_str device part of the file system
886 * @param fs_type tyep of the file system
887 * @param path path of the file to load.
888 * @return the loaded structure or NULL on failure.
889 */
890static struct key_program *load_key_chunk(const char *ifname,
891 const char *dev_part_str, int fs_type,
892 const char *path)
893{
894 struct key_program *result = NULL;
895 struct key_program header;
896 uint32_t crc;
897 uint8_t buf[12];
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800898 loff_t i;
Dirk Eibach762d3df2013-06-26 15:55:17 +0200899
900 if (fs_set_blk_dev(ifname, dev_part_str, fs_type))
901 goto failure;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800902 if (fs_read(path, (ulong)buf, 0, 12, &i) < 0)
903 goto failure;
Dirk Eibach762d3df2013-06-26 15:55:17 +0200904 if (i < 12)
905 goto failure;
906 header.magic = get_unaligned_be32(buf);
907 header.code_crc = get_unaligned_be32(buf + 4);
908 header.code_size = get_unaligned_be32(buf + 8);
909
910 if (header.magic != MAGIC_HMAC && header.magic != MAGIC_KEY_PROGRAM)
911 goto failure;
912
913 result = malloc(sizeof(struct key_program) + header.code_size);
914 if (!result)
915 goto failure;
916 if (fs_set_blk_dev(ifname, dev_part_str, fs_type))
917 goto failure;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800918 if (fs_read(path, (ulong)result, 0,
919 sizeof(struct key_program) + header.code_size, &i) < 0)
920 goto failure;
Dirk Eibach762d3df2013-06-26 15:55:17 +0200921 if (i <= 0)
922 goto failure;
923 *result = header;
924
925 crc = crc32(0, result->code, result->code_size);
926
927 if (crc != result->code_crc) {
928 printf("%s: HRC crc mismatch: %08x != %08x\n",
929 path, crc, result->code_crc);
930 goto failure;
931 }
932 goto end;
933failure:
934 if (result) {
935 free(result);
936 result = NULL;
937 }
938end:
939 return result;
940}
941#endif
942
943#if defined(CCDM_FIRST_STAGE) || (defined CCDM_AUTO_FIRST_STAGE)
Tom Rini910c7932017-06-16 13:06:26 -0400944static const uint8_t prg_stage1_prepare[] = {
945 0x00, 0x20, 0x00, 0x00, /* opcode: SYNC f0 */
946 0x00, 0x24, 0x00, 0x00, /* opcode: SYNC f1 */
947 0x01, 0x80, 0x00, 0x00, /* opcode: CHECK0 PCR0 */
948 0x81, 0x22, 0x00, 0x00, /* opcode: LOAD PCR0, f0 */
949 0x01, 0x84, 0x00, 0x00, /* opcode: CHECK0 PCR1 */
950 0x81, 0x26, 0x10, 0x00, /* opcode: LOAD PCR1, f1 */
951 0x01, 0x88, 0x00, 0x00, /* opcode: CHECK0 PCR2 */
952 0x81, 0x2a, 0x20, 0x00, /* opcode: LOAD PCR2, f2 */
953 0x01, 0x8c, 0x00, 0x00, /* opcode: CHECK0 PCR3 */
954 0x81, 0x2e, 0x30, 0x00, /* opcode: LOAD PCR3, f3 */
955};
956
Simon Glass8ceca1d2018-11-18 14:22:27 -0700957static int first_stage_actions(struct udevice *tpm)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200958{
959 int result = 0;
960 struct key_program *sd_prg = NULL;
961
962 puts("CCDM S1: start actions\n");
963#ifndef CCDM_SECOND_STAGE
Simon Glass8ceca1d2018-11-18 14:22:27 -0700964 if (tpm_continue_self_test(tpm))
Dirk Eibach762d3df2013-06-26 15:55:17 +0200965 goto failure;
966#else
Simon Glass8ceca1d2018-11-18 14:22:27 -0700967 tpm_continue_self_test(tpm);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200968#endif
969 mdelay(37);
970
Simon Glass8ceca1d2018-11-18 14:22:27 -0700971 if (hre_run_program(tpm, prg_stage1_prepare,
972 sizeof(prg_stage1_prepare)))
Dirk Eibach762d3df2013-06-26 15:55:17 +0200973 goto failure;
974
975 sd_prg = load_sd_key_program();
976 if (sd_prg) {
Simon Glass8ceca1d2018-11-18 14:22:27 -0700977 if (hre_run_program(tpm, sd_prg->code, sd_prg->code_size))
Dirk Eibach762d3df2013-06-26 15:55:17 +0200978 goto failure;
979 puts("SD code run successfully\n");
980 } else {
981 puts("no key program found on SD\n");
982 goto failure;
983 }
984 goto end;
985failure:
986 result = 1;
987end:
988 if (sd_prg)
989 free(sd_prg);
990 printf("CCDM S1: actions done (%d)\n", result);
991 return result;
992}
993#endif
994
995#ifdef CCDM_FIRST_STAGE
996static int first_stage_init(void)
997{
Simon Glass8ceca1d2018-11-18 14:22:27 -0700998 struct udevice *tpm;
999 int ret;
1000
Dirk Eibach762d3df2013-06-26 15:55:17 +02001001 puts("CCDM S1\n");
Simon Glass8ceca1d2018-11-18 14:22:27 -07001002 ret = get_tpm(&tpm);
1003 if (ret || tpm_init(tpm) || tpm_startup(tpm, TPM_ST_CLEAR))
Dirk Eibach762d3df2013-06-26 15:55:17 +02001004 return 1;
Simon Glass8ceca1d2018-11-18 14:22:27 -07001005 ret = first_stage_actions(tpm);
Dirk Eibach762d3df2013-06-26 15:55:17 +02001006#ifndef CCDM_SECOND_STAGE
Simon Glass8ceca1d2018-11-18 14:22:27 -07001007 if (!ret) {
Dirk Eibach762d3df2013-06-26 15:55:17 +02001008 if (bl2_entry)
1009 (*bl2_entry)();
Simon Glass8ceca1d2018-11-18 14:22:27 -07001010 ret = 1;
Dirk Eibach762d3df2013-06-26 15:55:17 +02001011 }
1012#endif
Simon Glass8ceca1d2018-11-18 14:22:27 -07001013 return ret;
Dirk Eibach762d3df2013-06-26 15:55:17 +02001014}
1015#endif
1016
1017#ifdef CCDM_SECOND_STAGE
Tom Rini09868d42017-05-08 22:14:26 -04001018static const uint8_t prg_stage2_prepare[] = {
1019 0x00, 0x80, 0x00, 0x00, /* opcode: SYNC PCR0 */
1020 0x00, 0x84, 0x00, 0x00, /* opcode: SYNC PCR1 */
1021 0x00, 0x88, 0x00, 0x00, /* opcode: SYNC PCR2 */
1022 0x00, 0x8c, 0x00, 0x00, /* opcode: SYNC PCR3 */
1023 0x00, 0x90, 0x00, 0x00, /* opcode: SYNC PCR4 */
1024};
1025
1026static const uint8_t prg_stage2_success[] = {
1027 0x81, 0x02, 0x40, 0x14, /* opcode: LOAD PCR4, #<20B data> */
1028 0x48, 0xfd, 0x95, 0x17, 0xe7, 0x54, 0x6b, 0x68, /* data */
1029 0x92, 0x31, 0x18, 0x05, 0xf8, 0x58, 0x58, 0x3c, /* data */
1030 0xe4, 0xd2, 0x81, 0xe0, /* data */
1031};
1032
1033static const uint8_t prg_stage_fail[] = {
1034 0x81, 0x01, 0x00, 0x14, /* opcode: LOAD v0, #<20B data> */
1035 0xc0, 0x32, 0xad, 0xc1, 0xff, 0x62, 0x9c, 0x9b, /* data */
1036 0x66, 0xf2, 0x27, 0x49, 0xad, 0x66, 0x7e, 0x6b, /* data */
1037 0xea, 0xdf, 0x14, 0x4b, /* data */
1038 0x81, 0x42, 0x30, 0x00, /* opcode: LOAD PCR3, v0 */
1039 0x81, 0x42, 0x40, 0x00, /* opcode: LOAD PCR4, v0 */
1040};
1041
Dirk Eibach762d3df2013-06-26 15:55:17 +02001042static int second_stage_init(void)
1043{
1044 static const char mac_suffix[] = ".mac";
1045 bool did_first_stage_run = true;
1046 int result = 0;
1047 char *cptr, *mmcdev = NULL;
1048 struct key_program *hmac_blob = NULL;
1049 const char *image_path = "/ccdm.itb";
1050 char *mac_path = NULL;
1051 ulong image_addr;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001052 loff_t image_size;
Simon Glass8ceca1d2018-11-18 14:22:27 -07001053 struct udevice *tpm;
Dirk Eibach762d3df2013-06-26 15:55:17 +02001054 uint32_t err;
Simon Glass8ceca1d2018-11-18 14:22:27 -07001055 int ret;
Dirk Eibach762d3df2013-06-26 15:55:17 +02001056
1057 printf("CCDM S2\n");
Simon Glass8ceca1d2018-11-18 14:22:27 -07001058 ret = get_tpm(&tpm);
1059 if (ret || tpm_init(tpm))
Dirk Eibach762d3df2013-06-26 15:55:17 +02001060 return 1;
Simon Glass8ceca1d2018-11-18 14:22:27 -07001061 err = tpm_startup(tpm, TPM_ST_CLEAR);
Dirk Eibach762d3df2013-06-26 15:55:17 +02001062 if (err != TPM_INVALID_POSTINIT)
1063 did_first_stage_run = false;
1064
1065#ifdef CCDM_AUTO_FIRST_STAGE
Simon Glass8ceca1d2018-11-18 14:22:27 -07001066 if (!did_first_stage_run && first_stage_actions(tpm))
Dirk Eibach762d3df2013-06-26 15:55:17 +02001067 goto failure;
1068#else
1069 if (!did_first_stage_run)
1070 goto failure;
1071#endif
1072
Simon Glass8ceca1d2018-11-18 14:22:27 -07001073 if (hre_run_program(tpm, prg_stage2_prepare,
1074 sizeof(prg_stage2_prepare)))
Dirk Eibach762d3df2013-06-26 15:55:17 +02001075 goto failure;
1076
1077 /* run "prepboot" from env to get "mmcdev" set */
Simon Glass64b723f2017-08-03 12:22:12 -06001078 cptr = env_get("prepboot");
Dirk Eibach762d3df2013-06-26 15:55:17 +02001079 if (cptr && !run_command(cptr, 0))
Simon Glass64b723f2017-08-03 12:22:12 -06001080 mmcdev = env_get("mmcdev");
Dirk Eibach762d3df2013-06-26 15:55:17 +02001081 if (!mmcdev)
1082 goto failure;
1083
Simon Glass64b723f2017-08-03 12:22:12 -06001084 cptr = env_get("ramdiskimage");
Dirk Eibach762d3df2013-06-26 15:55:17 +02001085 if (cptr)
1086 image_path = cptr;
1087
1088 mac_path = malloc(strlen(image_path) + strlen(mac_suffix) + 1);
1089 if (mac_path == NULL)
1090 goto failure;
1091 strcpy(mac_path, image_path);
1092 strcat(mac_path, mac_suffix);
1093
1094 /* read image from mmcdev (ccdm.itb) */
1095 image_addr = (ulong)get_image_location();
1096 if (fs_set_blk_dev("mmc", mmcdev, FS_TYPE_EXT))
1097 goto failure;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001098 if (fs_read(image_path, image_addr, 0, 0, &image_size) < 0)
1099 goto failure;
Dirk Eibach762d3df2013-06-26 15:55:17 +02001100 if (image_size <= 0)
1101 goto failure;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001102 printf("CCDM image found on %s, %lld bytes\n", mmcdev, image_size);
Dirk Eibach762d3df2013-06-26 15:55:17 +02001103
1104 hmac_blob = load_key_chunk("mmc", mmcdev, FS_TYPE_EXT, mac_path);
1105 if (!hmac_blob) {
1106 puts("failed to load mac file\n");
1107 goto failure;
1108 }
1109 if (verify_program(hmac_blob)) {
1110 puts("corrupted mac file\n");
1111 goto failure;
1112 }
1113 if (check_hmac(hmac_blob, (u8 *)image_addr, image_size)) {
1114 puts("image integrity could not be verified\n");
1115 goto failure;
1116 }
1117 puts("CCDM image OK\n");
1118
Simon Glass8ceca1d2018-11-18 14:22:27 -07001119 hre_run_program(tpm, prg_stage2_success, sizeof(prg_stage2_success));
Dirk Eibach762d3df2013-06-26 15:55:17 +02001120
1121 goto end;
1122failure:
1123 result = 1;
Simon Glass8ceca1d2018-11-18 14:22:27 -07001124 hre_run_program(tpm, prg_stage_fail, sizeof(prg_stage_fail));
Dirk Eibach762d3df2013-06-26 15:55:17 +02001125end:
1126 if (hmac_blob)
1127 free(hmac_blob);
1128 if (mac_path)
1129 free(mac_path);
1130
1131 return result;
1132}
1133#endif
1134
1135int show_self_hash(void)
1136{
1137 struct h_reg *hash_ptr;
1138#ifdef CCDM_SECOND_STAGE
1139 struct h_reg hash;
1140
1141 hash_ptr = &hash;
1142 if (compute_self_hash(hash_ptr))
1143 return 1;
1144#else
1145 hash_ptr = &fix_hregs[FIX_HREG_SELF_HASH];
1146#endif
1147 puts("self hash: ");
1148 if (hash_ptr && hash_ptr->valid)
1149 print_buffer(0, hash_ptr->digest, 1, 20, 20);
1150 else
1151 puts("INVALID\n");
1152
1153 return 0;
1154}
1155
1156/**
1157 * @brief let the system hang.
1158 *
1159 * Called on error.
1160 * Will stop the boot process; display a message and signal the error condition
1161 * by blinking the "status" and the "finder" LED of the controller board.
1162 *
1163 * @note the develop version runs the blink cycle 2 times and then returns.
1164 * The release version never returns.
1165 */
1166static void ccdm_hang(void)
1167{
1168 static const u64 f0 = 0x0ba3bb8ba2e880; /* blink code "finder" LED */
1169 static const u64 s0 = 0x00f0f0f0f0f0f0; /* blink code "status" LED */
1170 u64 f, s;
1171 int i;
1172#ifdef CCDM_DEVELOP
1173 int j;
1174#endif
1175
Dirk Eibache674bf12014-07-03 09:28:16 +02001176 I2C_SET_BUS(I2C_SOC_0);
Dirk Eibach762d3df2013-06-26 15:55:17 +02001177 pca9698_direction_output(0x22, 0, 0); /* Finder */
1178 pca9698_direction_output(0x22, 4, 0); /* Status */
1179
1180 puts("### ERROR ### Please RESET the board ###\n");
1181 bootstage_error(BOOTSTAGE_ID_NEED_RESET);
1182#ifdef CCDM_DEVELOP
1183 puts("*** ERROR ******** THIS WOULD HANG ******** ERROR ***\n");
1184 puts("** but we continue since this is a DEVELOP version **\n");
1185 puts("*** ERROR ******** THIS WOULD HANG ******** ERROR ***\n");
1186 for (j = 2; j-- > 0;) {
1187 putc('#');
1188#else
1189 for (;;) {
1190#endif
1191 f = f0;
1192 s = s0;
1193 for (i = 54; i-- > 0;) {
1194 pca9698_set_value(0x22, 0, !(f & 1));
1195 pca9698_set_value(0x22, 4, (s & 1));
1196 f >>= 1;
1197 s >>= 1;
1198 mdelay(120);
1199 }
1200 }
1201 puts("\ncontinue...\n");
1202}
1203
1204int startup_ccdm_id_module(void)
1205{
1206 int result = 0;
1207 unsigned int orig_i2c_bus;
1208
Dirk Eibache674bf12014-07-03 09:28:16 +02001209 orig_i2c_bus = i2c_get_bus_num();
1210 i2c_set_bus_num(I2C_SOC_1);
Dirk Eibach762d3df2013-06-26 15:55:17 +02001211
1212 /* goto end; */
1213
1214#ifdef CCDM_DEVELOP
1215 show_self_hash();
1216#endif
1217#ifdef CCDM_FIRST_STAGE
1218 result = first_stage_init();
1219 if (result) {
1220 puts("1st stage init failed\n");
1221 goto failure;
1222 }
1223#endif
1224#ifdef CCDM_SECOND_STAGE
1225 result = second_stage_init();
1226 if (result) {
1227 puts("2nd stage init failed\n");
1228 goto failure;
1229 }
1230#endif
1231
1232 goto end;
1233failure:
1234 result = 1;
1235end:
Dirk Eibache674bf12014-07-03 09:28:16 +02001236 i2c_set_bus_num(orig_i2c_bus);
Dirk Eibach762d3df2013-06-26 15:55:17 +02001237 if (result)
1238 ccdm_hang();
1239
1240 return result;
1241}