blob: 04d380956658901f632dd70108c8e540fb87cacc [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>
Dirk Eibach762d3df2013-06-26 15:55:17 +020017#include <malloc.h>
18#include <fs.h>
19#include <i2c.h>
20#include <mmc.h>
Miquel Raynal4c6759e2018-05-15 11:57:06 +020021#include <tpm-v1.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070022#include <u-boot/crc.h>
Jeroen Hofsteebfe88fe2014-06-12 22:27:12 +020023#include <u-boot/sha1.h>
Dirk Eibach762d3df2013-06-26 15:55:17 +020024#include <asm/byteorder.h>
25#include <asm/unaligned.h>
26#include <pca9698.h>
27
28#undef CCDM_FIRST_STAGE
29#undef CCDM_SECOND_STAGE
30#undef CCDM_AUTO_FIRST_STAGE
31
32#ifdef CONFIG_DEVELOP
33#define CCDM_DEVELOP
34#endif
35
36#ifdef CONFIG_TRAILBLAZER
37#define CCDM_FIRST_STAGE
38#undef CCDM_SECOND_STAGE
39#else
40#undef CCDM_FIRST_STAGE
41#define CCDM_SECOND_STAGE
42#endif
43
44#if defined(CCDM_DEVELOP) && defined(CCDM_SECOND_STAGE) && \
45 !defined(CCCM_FIRST_STAGE)
46#define CCDM_AUTO_FIRST_STAGE
47#endif
48
Dirk Eibach762d3df2013-06-26 15:55:17 +020049/* CCDM specific contants */
50enum {
51 /* NV indices */
52 NV_COMMON_DATA_INDEX = 0x40000001,
53 /* magics for key blob chains */
54 MAGIC_KEY_PROGRAM = 0x68726500,
55 MAGIC_HMAC = 0x68616300,
56 MAGIC_END_OF_CHAIN = 0x00000000,
57 /* sizes */
58 NV_COMMON_DATA_MIN_SIZE = 3 * sizeof(uint64_t) + 2 * sizeof(uint16_t),
59};
60
61/* other constants */
62enum {
63 ESDHC_BOOT_IMAGE_SIG_OFS = 0x40,
64 ESDHC_BOOT_IMAGE_SIZE_OFS = 0x48,
65 ESDHC_BOOT_IMAGE_ADDR_OFS = 0x50,
66 ESDHC_BOOT_IMAGE_TARGET_OFS = 0x58,
67 ESDHC_BOOT_IMAGE_ENTRY_OFS = 0x60,
68};
69
Dirk Eibache674bf12014-07-03 09:28:16 +020070enum {
71 I2C_SOC_0 = 0,
72 I2C_SOC_1 = 1,
73};
74
Dirk Eibach762d3df2013-06-26 15:55:17 +020075struct key_program {
76 uint32_t magic;
77 uint32_t code_crc;
78 uint32_t code_size;
79 uint8_t code[];
80};
81
82struct h_reg {
83 bool valid;
84 uint8_t digest[20];
85};
86
87
88enum access_mode {
89 HREG_NONE = 0,
90 HREG_RD = 1,
91 HREG_WR = 2,
92 HREG_RDWR = 3,
93};
94
95/* register constants */
96enum {
97 FIX_HREG_DEVICE_ID_HASH = 0,
98 FIX_HREG_SELF_HASH = 1,
99 FIX_HREG_STAGE2_HASH = 2,
100 FIX_HREG_VENDOR = 3,
101 COUNT_FIX_HREGS
102};
103
104
105/* hre opcodes */
106enum {
107 /* opcodes w/o data */
108 HRE_NOP = 0x00,
109 HRE_SYNC = HRE_NOP,
110 HRE_CHECK0 = 0x01,
111 /* opcodes w/o data, w/ sync dst */
112 /* opcodes w/ data */
113 HRE_LOAD = 0x81,
114 /* opcodes w/data, w/sync dst */
115 HRE_XOR = 0xC1,
116 HRE_AND = 0xC2,
117 HRE_OR = 0xC3,
118 HRE_EXTEND = 0xC4,
119 HRE_LOADKEY = 0xC5,
120};
121
122/* hre errors */
123enum {
124 HRE_E_OK = 0,
125 HRE_E_TPM_FAILURE,
126 HRE_E_INVALID_HREG,
127};
128
129static uint64_t device_id;
130static uint64_t device_cl;
131static uint64_t device_type;
132
133static uint32_t platform_key_handle;
134
135static void(*bl2_entry)(void);
136
137static struct h_reg pcr_hregs[24];
138static struct h_reg fix_hregs[COUNT_FIX_HREGS];
139static struct h_reg var_hregs[8];
140static uint32_t hre_tpm_err;
141static int hre_err = HRE_E_OK;
142
143#define IS_PCR_HREG(spec) ((spec) & 0x20)
144#define IS_FIX_HREG(spec) (((spec) & 0x38) == 0x08)
145#define IS_VAR_HREG(spec) (((spec) & 0x38) == 0x10)
146#define HREG_IDX(spec) ((spec) & (IS_PCR_HREG(spec) ? 0x1f : 0x7))
147
Simon Glass8ceca1d2018-11-18 14:22:27 -0700148static int get_tpm(struct udevice **devp)
149{
150 int rc;
151
152 rc = uclass_first_device_err(UCLASS_TPM, devp);
153 if (rc) {
154 printf("Could not find TPM (ret=%d)\n", rc);
155 return CMD_RET_FAILURE;
156 }
157
158 return 0;
159}
160
Dirk Eibach762d3df2013-06-26 15:55:17 +0200161static const uint8_t vendor[] = "Guntermann & Drunck";
162
Dirk Eibach762d3df2013-06-26 15:55:17 +0200163/**
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 */
172static 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
179 if (size <= 0)
180 goto end;
181
182 blk_len = mmc->read_bl_len;
183 tmp_buf = malloc(blk_len);
184 if (!tmp_buf)
185 goto failure;
186 block_no = src / blk_len;
187 ofs = src % blk_len;
188
189 if (ofs) {
Stephen Warrene73f2962015-12-07 11:38:48 -0700190 n = mmc->block_dev.block_read(&mmc->block_dev, block_no++, 1,
Dirk Eibach762d3df2013-06-26 15:55:17 +0200191 tmp_buf);
192 if (!n)
193 goto failure;
Masahiro Yamadadb204642014-11-07 03:03:31 +0900194 result = min(size, (int)(blk_len - ofs));
Dirk Eibach762d3df2013-06-26 15:55:17 +0200195 memcpy(dst, tmp_buf + ofs, result);
196 dst += result;
197 size -= result;
198 }
199 cnt = size / blk_len;
200 if (cnt) {
Stephen Warrene73f2962015-12-07 11:38:48 -0700201 n = mmc->block_dev.block_read(&mmc->block_dev, block_no, cnt,
Dirk Eibach762d3df2013-06-26 15:55:17 +0200202 dst);
203 if (n != cnt)
204 goto failure;
205 size -= cnt * blk_len;
206 result += cnt * blk_len;
207 dst += cnt * blk_len;
208 block_no += cnt;
209 }
210 if (size) {
Stephen Warrene73f2962015-12-07 11:38:48 -0700211 n = mmc->block_dev.block_read(&mmc->block_dev, block_no++, 1,
Dirk Eibach762d3df2013-06-26 15:55:17 +0200212 tmp_buf);
213 if (!n)
214 goto failure;
215 memcpy(dst, tmp_buf, size);
216 result += size;
217 }
218 goto end;
219failure:
220 result = -1;
221end:
222 if (tmp_buf)
223 free(tmp_buf);
224 return result;
225}
226
227/**
228 * @brief returns a location where the 2nd stage bootloader can be(/ is) placed.
229 *
230 * @return pointer to the location for/of the 2nd stage bootloader
231 */
232static u8 *get_2nd_stage_bl_location(ulong target_addr)
233{
234 ulong addr;
235#ifdef CCDM_SECOND_STAGE
Simon Glass22c34c22017-08-03 12:22:13 -0600236 addr = env_get_ulong("loadaddr", 16, CONFIG_LOADADDR);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200237#else
238 addr = target_addr;
239#endif
240 return (u8 *)(addr);
241}
242
243
244#ifdef CCDM_SECOND_STAGE
245/**
246 * @brief returns a location where the image can be(/ is) placed.
247 *
248 * @return pointer to the location for/of the image
249 */
250static u8 *get_image_location(void)
251{
252 ulong addr;
253 /* TODO use other area? */
Simon Glass22c34c22017-08-03 12:22:13 -0600254 addr = env_get_ulong("loadaddr", 16, CONFIG_LOADADDR);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200255 return (u8 *)(addr);
256}
257#endif
258
259/**
260 * @brief get the size of a given (TPM) NV area
261 * @param index NV index of the area to get size for
262 * @param size pointer to the size
263 * @return 0 on success, != 0 on error
264 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700265static int get_tpm_nv_size(struct udevice *tpm, uint32_t index, uint32_t *size)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200266{
267 uint32_t err;
268 uint8_t info[72];
269 uint8_t *ptr;
270 uint16_t v16;
271
Simon Glass8ceca1d2018-11-18 14:22:27 -0700272 err = tpm_get_capability(tpm, TPM_CAP_NV_INDEX, index,
273 info, sizeof(info));
Dirk Eibach762d3df2013-06-26 15:55:17 +0200274 if (err) {
275 printf("tpm_get_capability(CAP_NV_INDEX, %08x) failed: %u\n",
276 index, err);
277 return 1;
278 }
279
280 /* skip tag and nvIndex */
281 ptr = info + 6;
282 /* skip 2 pcr info fields */
283 v16 = get_unaligned_be16(ptr);
284 ptr += 2 + v16 + 1 + 20;
285 v16 = get_unaligned_be16(ptr);
286 ptr += 2 + v16 + 1 + 20;
287 /* skip permission and flags */
288 ptr += 6 + 3;
289
290 *size = get_unaligned_be32(ptr);
291 return 0;
292}
293
294/**
295 * @brief search for a key by usage auth and pub key hash.
296 * @param auth usage auth of the key to search for
297 * @param pubkey_digest (SHA1) hash of the pub key structure of the key
298 * @param[out] handle the handle of the key iff found
299 * @return 0 if key was found in TPM; != 0 if not.
300 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700301static int find_key(struct udevice *tpm, const uint8_t auth[20],
302 const uint8_t pubkey_digest[20], uint32_t *handle)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200303{
304 uint16_t key_count;
305 uint32_t key_handles[10];
306 uint8_t buf[288];
307 uint8_t *ptr;
308 uint32_t err;
309 uint8_t digest[20];
310 size_t buf_len;
311 unsigned int i;
312
313 /* fetch list of already loaded keys in the TPM */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700314 err = tpm_get_capability(tpm, TPM_CAP_HANDLE, TPM_RT_KEY, buf,
315 sizeof(buf));
Dirk Eibach762d3df2013-06-26 15:55:17 +0200316 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
323 /* now search a(/ the) key which we can access with the given auth */
324 for (i = 0; i < key_count; ++i) {
325 buf_len = sizeof(buf);
Simon Glass8ceca1d2018-11-18 14:22:27 -0700326 err = tpm_get_pub_key_oiap(tpm, key_handles[i], auth, buf,
327 &buf_len);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200328 if (err && err != TPM_AUTHFAIL)
329 return -1;
330 if (err)
331 continue;
332 sha1_csum(buf, buf_len, digest);
333 if (!memcmp(digest, pubkey_digest, 20)) {
334 *handle = key_handles[i];
335 return 0;
336 }
337 }
338 return 1;
339}
340
341/**
342 * @brief read CCDM common data from TPM NV
343 * @return 0 if CCDM common data was found and read, !=0 if something failed.
344 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700345static int read_common_data(struct udevice *tpm)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200346{
347 uint32_t size;
348 uint32_t err;
349 uint8_t buf[256];
350 sha1_context ctx;
351
Simon Glass8ceca1d2018-11-18 14:22:27 -0700352 if (get_tpm_nv_size(tpm, NV_COMMON_DATA_INDEX, &size) ||
Dirk Eibach762d3df2013-06-26 15:55:17 +0200353 size < NV_COMMON_DATA_MIN_SIZE)
354 return 1;
Simon Glass8ceca1d2018-11-18 14:22:27 -0700355 err = tpm_nv_read_value(tpm, NV_COMMON_DATA_INDEX,
356 buf, min(sizeof(buf), size));
Dirk Eibach762d3df2013-06-26 15:55:17 +0200357 if (err) {
358 printf("tpm_nv_read_value() failed: %u\n", err);
359 return 1;
360 }
361
362 device_id = get_unaligned_be64(buf);
363 device_cl = get_unaligned_be64(buf + 8);
364 device_type = get_unaligned_be64(buf + 16);
365
366 sha1_starts(&ctx);
367 sha1_update(&ctx, buf, 24);
368 sha1_finish(&ctx, fix_hregs[FIX_HREG_DEVICE_ID_HASH].digest);
369 fix_hregs[FIX_HREG_DEVICE_ID_HASH].valid = true;
370
371 platform_key_handle = get_unaligned_be32(buf + 24);
372
373 return 0;
374}
375
376/**
377 * @brief compute hash of bootloader itself.
378 * @param[out] dst hash register where the hash should be stored
379 * @return 0 on success, != 0 on failure.
380 *
381 * @note MUST be called at a time where the boot loader is accessible at the
382 * configured location (; so take care when code is reallocated).
383 */
384static int compute_self_hash(struct h_reg *dst)
385{
386 sha1_csum((const uint8_t *)CONFIG_SYS_MONITOR_BASE,
387 CONFIG_SYS_MONITOR_LEN, dst->digest);
388 dst->valid = true;
389 return 0;
390}
391
392int ccdm_compute_self_hash(void)
393{
394 if (!fix_hregs[FIX_HREG_SELF_HASH].valid)
395 compute_self_hash(&fix_hregs[FIX_HREG_SELF_HASH]);
396 return 0;
397}
398
399/**
400 * @brief compute the hash of the 2nd stage boot loader (on SD card)
401 * @param[out] dst hash register to store the computed hash
402 * @return 0 on success, != 0 on failure
403 *
404 * Determines the size and location of the 2nd stage boot loader on SD card,
405 * loads the 2nd stage boot loader and computes the (SHA1) hash value.
406 * Within the 1st stage boot loader, the 2nd stage boot loader is loaded at
407 * the desired memory location and the variable @a bl2_entry is set.
408 *
409 * @note This sets the variable @a bl2_entry to the entry point when the
410 * 2nd stage boot loader is loaded at its configured memory location.
411 */
412static int compute_second_stage_hash(struct h_reg *dst)
413{
414 int result = 0;
415 u32 code_len, code_offset, target_addr, exec_entry;
416 struct mmc *mmc;
417 u8 *load_addr = NULL;
418 u8 buf[128];
419
420 mmc = find_mmc_device(0);
421 if (!mmc)
422 goto failure;
423 mmc_init(mmc);
424
425 if (ccdm_mmc_read(mmc, 0, buf, sizeof(buf)) < 0)
426 goto failure;
427
428 code_offset = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ADDR_OFS);
429 code_len = *(u32 *)(buf + ESDHC_BOOT_IMAGE_SIZE_OFS);
430 target_addr = *(u32 *)(buf + ESDHC_BOOT_IMAGE_TARGET_OFS);
431 exec_entry = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ENTRY_OFS);
432
433 load_addr = get_2nd_stage_bl_location(target_addr);
434 if (load_addr == (u8 *)target_addr)
435 bl2_entry = (void(*)(void))exec_entry;
436
437 if (ccdm_mmc_read(mmc, code_offset, load_addr, code_len) < 0)
438 goto failure;
439
440 sha1_csum(load_addr, code_len, dst->digest);
441 dst->valid = true;
442
443 goto end;
444failure:
445 result = 1;
446 bl2_entry = NULL;
447end:
448 return result;
449}
450
451/**
452 * @brief get pointer to hash register by specification
453 * @param spec specification of a hash register
454 * @return pointer to hash register or NULL if @a spec does not qualify a
455 * valid hash register; NULL else.
456 */
457static struct h_reg *get_hreg(uint8_t spec)
458{
459 uint8_t idx;
460
461 idx = HREG_IDX(spec);
462 if (IS_FIX_HREG(spec)) {
463 if (idx < ARRAY_SIZE(fix_hregs))
464 return fix_hregs + idx;
465 hre_err = HRE_E_INVALID_HREG;
466 } else if (IS_PCR_HREG(spec)) {
467 if (idx < ARRAY_SIZE(pcr_hregs))
468 return pcr_hregs + idx;
469 hre_err = HRE_E_INVALID_HREG;
470 } else if (IS_VAR_HREG(spec)) {
471 if (idx < ARRAY_SIZE(var_hregs))
472 return var_hregs + idx;
473 hre_err = HRE_E_INVALID_HREG;
474 }
475 return NULL;
476}
477
478/**
479 * @brief get pointer of a hash register by specification and usage.
480 * @param spec specification of a hash register
481 * @param mode access mode (read or write or read/write)
482 * @return pointer to hash register if found and valid; NULL else.
483 *
484 * This func uses @a get_reg() to determine the hash register for a given spec.
485 * If a register is found it is validated according to the desired access mode.
486 * The value of automatic registers (PCR register and fixed registers) is
487 * loaded or computed on read access.
488 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700489static struct h_reg *access_hreg(struct udevice *tpm, uint8_t spec,
490 enum access_mode mode)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200491{
492 struct h_reg *result;
493
494 result = get_hreg(spec);
495 if (!result)
496 return NULL;
497
498 if (mode & HREG_WR) {
499 if (IS_FIX_HREG(spec)) {
500 hre_err = HRE_E_INVALID_HREG;
501 return NULL;
502 }
503 }
504 if (mode & HREG_RD) {
505 if (!result->valid) {
506 if (IS_PCR_HREG(spec)) {
Simon Glass8ceca1d2018-11-18 14:22:27 -0700507 hre_tpm_err = tpm_pcr_read(tpm, HREG_IDX(spec),
Dirk Eibach762d3df2013-06-26 15:55:17 +0200508 result->digest, 20);
509 result->valid = (hre_tpm_err == TPM_SUCCESS);
510 } else if (IS_FIX_HREG(spec)) {
511 switch (HREG_IDX(spec)) {
512 case FIX_HREG_DEVICE_ID_HASH:
Simon Glass8ceca1d2018-11-18 14:22:27 -0700513 read_common_data(tpm);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200514 break;
515 case FIX_HREG_SELF_HASH:
516 ccdm_compute_self_hash();
517 break;
518 case FIX_HREG_STAGE2_HASH:
519 compute_second_stage_hash(result);
520 break;
521 case FIX_HREG_VENDOR:
522 memcpy(result->digest, vendor, 20);
523 result->valid = true;
524 break;
525 }
526 } else {
527 result->valid = true;
528 }
529 }
530 if (!result->valid) {
531 hre_err = HRE_E_INVALID_HREG;
532 return NULL;
533 }
534 }
535
536 return result;
537}
538
539static void *compute_and(void *_dst, const void *_src, size_t n)
540{
541 uint8_t *dst = _dst;
542 const uint8_t *src = _src;
543 size_t i;
544
545 for (i = n; i-- > 0; )
546 *dst++ &= *src++;
547
548 return _dst;
549}
550
551static void *compute_or(void *_dst, const void *_src, size_t n)
552{
553 uint8_t *dst = _dst;
554 const uint8_t *src = _src;
555 size_t i;
556
557 for (i = n; i-- > 0; )
558 *dst++ |= *src++;
559
560 return _dst;
561}
562
563static void *compute_xor(void *_dst, const void *_src, size_t n)
564{
565 uint8_t *dst = _dst;
566 const uint8_t *src = _src;
567 size_t i;
568
569 for (i = n; i-- > 0; )
570 *dst++ ^= *src++;
571
572 return _dst;
573}
574
575static void *compute_extend(void *_dst, const void *_src, size_t n)
576{
577 uint8_t digest[20];
578 sha1_context ctx;
579
580 sha1_starts(&ctx);
581 sha1_update(&ctx, _dst, n);
582 sha1_update(&ctx, _src, n);
583 sha1_finish(&ctx, digest);
584 memcpy(_dst, digest, min(n, sizeof(digest)));
585
586 return _dst;
587}
588
Simon Glass8ceca1d2018-11-18 14:22:27 -0700589static int hre_op_loadkey(struct udevice *tpm, struct h_reg *src_reg,
590 struct h_reg *dst_reg, const void *key,
591 size_t key_size)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200592{
593 uint32_t parent_handle;
594 uint32_t key_handle;
595
596 if (!src_reg || !dst_reg || !src_reg->valid || !dst_reg->valid)
597 return -1;
Simon Glass8ceca1d2018-11-18 14:22:27 -0700598 if (find_key(tpm, src_reg->digest, dst_reg->digest, &parent_handle))
Dirk Eibach762d3df2013-06-26 15:55:17 +0200599 return -1;
Simon Glass8ceca1d2018-11-18 14:22:27 -0700600 hre_tpm_err = tpm_load_key2_oiap(tpm, parent_handle, key, key_size,
601 src_reg->digest, &key_handle);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200602 if (hre_tpm_err) {
603 hre_err = HRE_E_TPM_FAILURE;
604 return -1;
605 }
606 /* TODO remember key handle somehow? */
607
608 return 0;
609}
610
611/**
612 * @brief executes the next opcode on the hash register engine.
613 * @param[in,out] ip pointer to the opcode (instruction pointer)
614 * @param[in,out] code_size (remaining) size of the code
615 * @return new instruction pointer on success, NULL on error.
616 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700617static const uint8_t *hre_execute_op(struct udevice *tpm, const uint8_t **ip,
618 size_t *code_size)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200619{
620 bool dst_modified = false;
621 uint32_t ins;
622 uint8_t opcode;
623 uint8_t src_spec;
624 uint8_t dst_spec;
625 uint16_t data_size;
626 struct h_reg *src_reg, *dst_reg;
627 uint8_t buf[20];
628 const uint8_t *src_buf, *data;
629 uint8_t *ptr;
630 int i;
631 void * (*bin_func)(void *, const void *, size_t);
632
633 if (*code_size < 4)
634 return NULL;
635
636 ins = get_unaligned_be32(*ip);
637 opcode = **ip;
638 data = *ip + 4;
639 src_spec = (ins >> 18) & 0x3f;
640 dst_spec = (ins >> 12) & 0x3f;
641 data_size = (ins & 0x7ff);
642
643 debug("HRE: ins=%08x (op=%02x, s=%02x, d=%02x, L=%d)\n", ins,
644 opcode, src_spec, dst_spec, data_size);
645
646 if ((opcode & 0x80) && (data_size + 4) > *code_size)
647 return NULL;
648
Simon Glass8ceca1d2018-11-18 14:22:27 -0700649 src_reg = access_hreg(tpm, src_spec, HREG_RD);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200650 if (hre_err || hre_tpm_err)
651 return NULL;
Simon Glass8ceca1d2018-11-18 14:22:27 -0700652 dst_reg = access_hreg(tpm, dst_spec,
653 (opcode & 0x40) ? HREG_RDWR : HREG_WR);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200654 if (hre_err || hre_tpm_err)
655 return NULL;
656
657 switch (opcode) {
658 case HRE_NOP:
659 goto end;
660 case HRE_CHECK0:
661 if (src_reg) {
662 for (i = 0; i < 20; ++i) {
663 if (src_reg->digest[i])
664 return NULL;
665 }
666 }
667 break;
668 case HRE_LOAD:
669 bin_func = memcpy;
670 goto do_bin_func;
671 case HRE_XOR:
672 bin_func = compute_xor;
673 goto do_bin_func;
674 case HRE_AND:
675 bin_func = compute_and;
676 goto do_bin_func;
677 case HRE_OR:
678 bin_func = compute_or;
679 goto do_bin_func;
680 case HRE_EXTEND:
681 bin_func = compute_extend;
682do_bin_func:
683 if (!dst_reg)
684 return NULL;
685 if (src_reg) {
686 src_buf = src_reg->digest;
687 } else {
688 if (!data_size) {
689 memset(buf, 0, 20);
690 src_buf = buf;
691 } else if (data_size == 1) {
692 memset(buf, *data, 20);
693 src_buf = buf;
694 } else if (data_size >= 20) {
695 src_buf = data;
696 } else {
697 src_buf = buf;
698 for (ptr = (uint8_t *)src_buf, i = 20; i > 0;
699 i -= data_size, ptr += data_size)
Masahiro Yamadadb204642014-11-07 03:03:31 +0900700 memcpy(ptr, data,
701 min_t(size_t, i, data_size));
Dirk Eibach762d3df2013-06-26 15:55:17 +0200702 }
703 }
704 bin_func(dst_reg->digest, src_buf, 20);
705 dst_reg->valid = true;
706 dst_modified = true;
707 break;
708 case HRE_LOADKEY:
Simon Glass8ceca1d2018-11-18 14:22:27 -0700709 if (hre_op_loadkey(tpm, src_reg, dst_reg, data, data_size))
Dirk Eibach762d3df2013-06-26 15:55:17 +0200710 return NULL;
711 break;
712 default:
713 return NULL;
714 }
715
716 if (dst_reg && dst_modified && IS_PCR_HREG(dst_spec)) {
Simon Glass8ceca1d2018-11-18 14:22:27 -0700717 hre_tpm_err = tpm_extend(tpm, HREG_IDX(dst_spec),
718 dst_reg->digest, dst_reg->digest);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200719 if (hre_tpm_err) {
720 hre_err = HRE_E_TPM_FAILURE;
721 return NULL;
722 }
723 }
724end:
725 *ip += 4;
726 *code_size -= 4;
727 if (opcode & 0x80) {
728 *ip += data_size;
729 *code_size -= data_size;
730 }
731
732 return *ip;
733}
734
735/**
736 * @brief runs a program on the hash register engine.
737 * @param code pointer to the (HRE) code.
738 * @param code_size size of the code (in bytes).
739 * @return 0 on success, != 0 on failure.
740 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700741static int hre_run_program(struct udevice *tpm, const uint8_t *code,
742 size_t code_size)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200743{
744 size_t code_left;
745 const uint8_t *ip = code;
746
747 code_left = code_size;
748 hre_tpm_err = 0;
749 hre_err = HRE_E_OK;
750 while (code_left > 0)
Simon Glass8ceca1d2018-11-18 14:22:27 -0700751 if (!hre_execute_op(tpm, &ip, &code_left))
Dirk Eibach762d3df2013-06-26 15:55:17 +0200752 return -1;
753
754 return hre_err;
755}
756
757static int check_hmac(struct key_program *hmac,
758 const uint8_t *data, size_t data_size)
759{
760 uint8_t key[20], computed_hmac[20];
761 uint32_t type;
762
763 type = get_unaligned_be32(hmac->code);
764 if (type != 0)
765 return 1;
766 memset(key, 0, sizeof(key));
767 compute_extend(key, pcr_hregs[1].digest, 20);
768 compute_extend(key, pcr_hregs[2].digest, 20);
769 compute_extend(key, pcr_hregs[3].digest, 20);
770 compute_extend(key, pcr_hregs[4].digest, 20);
771
772 sha1_hmac(key, sizeof(key), data, data_size, computed_hmac);
773
774 return memcmp(computed_hmac, hmac->code + 4, 20);
775}
776
777static int verify_program(struct key_program *prg)
778{
779 uint32_t crc;
780 crc = crc32(0, prg->code, prg->code_size);
781
782 if (crc != prg->code_crc) {
783 printf("HRC crc mismatch: %08x != %08x\n",
784 crc, prg->code_crc);
785 return 1;
786 }
787 return 0;
788}
789
790#if defined(CCDM_FIRST_STAGE) || (defined CCDM_AUTO_FIRST_STAGE)
791static struct key_program *load_sd_key_program(void)
792{
793 u32 code_len, code_offset;
794 struct mmc *mmc;
795 u8 buf[128];
796 struct key_program *result = NULL, *hmac = NULL;
797 struct key_program header;
798
799 mmc = find_mmc_device(0);
800 if (!mmc)
801 return NULL;
802 mmc_init(mmc);
803
804 if (ccdm_mmc_read(mmc, 0, buf, sizeof(buf)) <= 0)
805 goto failure;
806
807 code_offset = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ADDR_OFS);
808 code_len = *(u32 *)(buf + ESDHC_BOOT_IMAGE_SIZE_OFS);
809
810 code_offset += code_len;
811 /* TODO: the following needs to be the size of the 2nd stage env */
812 code_offset += CONFIG_ENV_SIZE;
813
814 if (ccdm_mmc_read(mmc, code_offset, buf, 4*3) < 0)
815 goto failure;
816
817 header.magic = get_unaligned_be32(buf);
818 header.code_crc = get_unaligned_be32(buf + 4);
819 header.code_size = get_unaligned_be32(buf + 8);
820
821 if (header.magic != MAGIC_KEY_PROGRAM)
822 goto failure;
823
824 result = malloc(sizeof(struct key_program) + header.code_size);
825 if (!result)
826 goto failure;
827 *result = header;
828
829 printf("load key program chunk from SD card (%u bytes) ",
830 header.code_size);
831 code_offset += 12;
832 if (ccdm_mmc_read(mmc, code_offset, result->code, header.code_size)
833 < 0)
834 goto failure;
835 code_offset += header.code_size;
836 puts("\n");
837
838 if (verify_program(result))
839 goto failure;
840
841 if (ccdm_mmc_read(mmc, code_offset, buf, 4*3) < 0)
842 goto failure;
843
844 header.magic = get_unaligned_be32(buf);
845 header.code_crc = get_unaligned_be32(buf + 4);
846 header.code_size = get_unaligned_be32(buf + 8);
847
848 if (header.magic == MAGIC_HMAC) {
849 puts("check integrity\n");
850 hmac = malloc(sizeof(struct key_program) + header.code_size);
851 if (!hmac)
852 goto failure;
853 *hmac = header;
854 code_offset += 12;
855 if (ccdm_mmc_read(mmc, code_offset, hmac->code,
856 hmac->code_size) < 0)
857 goto failure;
858 if (verify_program(hmac))
859 goto failure;
860 if (check_hmac(hmac, result->code, result->code_size)) {
861 puts("key program integrity could not be verified\n");
862 goto failure;
863 }
864 puts("key program verified\n");
865 }
866
867 goto end;
868failure:
869 if (result)
870 free(result);
871 result = NULL;
872end:
873 if (hmac)
874 free(hmac);
875
876 return result;
877}
878#endif
879
880#ifdef CCDM_SECOND_STAGE
881/**
882 * @brief load a key program from file system.
883 * @param ifname interface of the file system
884 * @param dev_part_str device part of the file system
885 * @param fs_type tyep of the file system
886 * @param path path of the file to load.
887 * @return the loaded structure or NULL on failure.
888 */
889static struct key_program *load_key_chunk(const char *ifname,
890 const char *dev_part_str, int fs_type,
891 const char *path)
892{
893 struct key_program *result = NULL;
894 struct key_program header;
895 uint32_t crc;
896 uint8_t buf[12];
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800897 loff_t i;
Dirk Eibach762d3df2013-06-26 15:55:17 +0200898
899 if (fs_set_blk_dev(ifname, dev_part_str, fs_type))
900 goto failure;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800901 if (fs_read(path, (ulong)buf, 0, 12, &i) < 0)
902 goto failure;
Dirk Eibach762d3df2013-06-26 15:55:17 +0200903 if (i < 12)
904 goto failure;
905 header.magic = get_unaligned_be32(buf);
906 header.code_crc = get_unaligned_be32(buf + 4);
907 header.code_size = get_unaligned_be32(buf + 8);
908
909 if (header.magic != MAGIC_HMAC && header.magic != MAGIC_KEY_PROGRAM)
910 goto failure;
911
912 result = malloc(sizeof(struct key_program) + header.code_size);
913 if (!result)
914 goto failure;
915 if (fs_set_blk_dev(ifname, dev_part_str, fs_type))
916 goto failure;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800917 if (fs_read(path, (ulong)result, 0,
918 sizeof(struct key_program) + header.code_size, &i) < 0)
919 goto failure;
Dirk Eibach762d3df2013-06-26 15:55:17 +0200920 if (i <= 0)
921 goto failure;
922 *result = header;
923
924 crc = crc32(0, result->code, result->code_size);
925
926 if (crc != result->code_crc) {
927 printf("%s: HRC crc mismatch: %08x != %08x\n",
928 path, crc, result->code_crc);
929 goto failure;
930 }
931 goto end;
932failure:
933 if (result) {
934 free(result);
935 result = NULL;
936 }
937end:
938 return result;
939}
940#endif
941
942#if defined(CCDM_FIRST_STAGE) || (defined CCDM_AUTO_FIRST_STAGE)
Tom Rini910c7932017-06-16 13:06:26 -0400943static const uint8_t prg_stage1_prepare[] = {
944 0x00, 0x20, 0x00, 0x00, /* opcode: SYNC f0 */
945 0x00, 0x24, 0x00, 0x00, /* opcode: SYNC f1 */
946 0x01, 0x80, 0x00, 0x00, /* opcode: CHECK0 PCR0 */
947 0x81, 0x22, 0x00, 0x00, /* opcode: LOAD PCR0, f0 */
948 0x01, 0x84, 0x00, 0x00, /* opcode: CHECK0 PCR1 */
949 0x81, 0x26, 0x10, 0x00, /* opcode: LOAD PCR1, f1 */
950 0x01, 0x88, 0x00, 0x00, /* opcode: CHECK0 PCR2 */
951 0x81, 0x2a, 0x20, 0x00, /* opcode: LOAD PCR2, f2 */
952 0x01, 0x8c, 0x00, 0x00, /* opcode: CHECK0 PCR3 */
953 0x81, 0x2e, 0x30, 0x00, /* opcode: LOAD PCR3, f3 */
954};
955
Simon Glass8ceca1d2018-11-18 14:22:27 -0700956static int first_stage_actions(struct udevice *tpm)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200957{
958 int result = 0;
959 struct key_program *sd_prg = NULL;
960
961 puts("CCDM S1: start actions\n");
962#ifndef CCDM_SECOND_STAGE
Simon Glass8ceca1d2018-11-18 14:22:27 -0700963 if (tpm_continue_self_test(tpm))
Dirk Eibach762d3df2013-06-26 15:55:17 +0200964 goto failure;
965#else
Simon Glass8ceca1d2018-11-18 14:22:27 -0700966 tpm_continue_self_test(tpm);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200967#endif
968 mdelay(37);
969
Simon Glass8ceca1d2018-11-18 14:22:27 -0700970 if (hre_run_program(tpm, prg_stage1_prepare,
971 sizeof(prg_stage1_prepare)))
Dirk Eibach762d3df2013-06-26 15:55:17 +0200972 goto failure;
973
974 sd_prg = load_sd_key_program();
975 if (sd_prg) {
Simon Glass8ceca1d2018-11-18 14:22:27 -0700976 if (hre_run_program(tpm, sd_prg->code, sd_prg->code_size))
Dirk Eibach762d3df2013-06-26 15:55:17 +0200977 goto failure;
978 puts("SD code run successfully\n");
979 } else {
980 puts("no key program found on SD\n");
981 goto failure;
982 }
983 goto end;
984failure:
985 result = 1;
986end:
987 if (sd_prg)
988 free(sd_prg);
989 printf("CCDM S1: actions done (%d)\n", result);
990 return result;
991}
992#endif
993
994#ifdef CCDM_FIRST_STAGE
995static int first_stage_init(void)
996{
Simon Glass8ceca1d2018-11-18 14:22:27 -0700997 struct udevice *tpm;
998 int ret;
999
Dirk Eibach762d3df2013-06-26 15:55:17 +02001000 puts("CCDM S1\n");
Simon Glass8ceca1d2018-11-18 14:22:27 -07001001 ret = get_tpm(&tpm);
1002 if (ret || tpm_init(tpm) || tpm_startup(tpm, TPM_ST_CLEAR))
Dirk Eibach762d3df2013-06-26 15:55:17 +02001003 return 1;
Simon Glass8ceca1d2018-11-18 14:22:27 -07001004 ret = first_stage_actions(tpm);
Dirk Eibach762d3df2013-06-26 15:55:17 +02001005#ifndef CCDM_SECOND_STAGE
Simon Glass8ceca1d2018-11-18 14:22:27 -07001006 if (!ret) {
Dirk Eibach762d3df2013-06-26 15:55:17 +02001007 if (bl2_entry)
1008 (*bl2_entry)();
Simon Glass8ceca1d2018-11-18 14:22:27 -07001009 ret = 1;
Dirk Eibach762d3df2013-06-26 15:55:17 +02001010 }
1011#endif
Simon Glass8ceca1d2018-11-18 14:22:27 -07001012 return ret;
Dirk Eibach762d3df2013-06-26 15:55:17 +02001013}
1014#endif
1015
1016#ifdef CCDM_SECOND_STAGE
Tom Rini09868d42017-05-08 22:14:26 -04001017static const uint8_t prg_stage2_prepare[] = {
1018 0x00, 0x80, 0x00, 0x00, /* opcode: SYNC PCR0 */
1019 0x00, 0x84, 0x00, 0x00, /* opcode: SYNC PCR1 */
1020 0x00, 0x88, 0x00, 0x00, /* opcode: SYNC PCR2 */
1021 0x00, 0x8c, 0x00, 0x00, /* opcode: SYNC PCR3 */
1022 0x00, 0x90, 0x00, 0x00, /* opcode: SYNC PCR4 */
1023};
1024
1025static const uint8_t prg_stage2_success[] = {
1026 0x81, 0x02, 0x40, 0x14, /* opcode: LOAD PCR4, #<20B data> */
1027 0x48, 0xfd, 0x95, 0x17, 0xe7, 0x54, 0x6b, 0x68, /* data */
1028 0x92, 0x31, 0x18, 0x05, 0xf8, 0x58, 0x58, 0x3c, /* data */
1029 0xe4, 0xd2, 0x81, 0xe0, /* data */
1030};
1031
1032static const uint8_t prg_stage_fail[] = {
1033 0x81, 0x01, 0x00, 0x14, /* opcode: LOAD v0, #<20B data> */
1034 0xc0, 0x32, 0xad, 0xc1, 0xff, 0x62, 0x9c, 0x9b, /* data */
1035 0x66, 0xf2, 0x27, 0x49, 0xad, 0x66, 0x7e, 0x6b, /* data */
1036 0xea, 0xdf, 0x14, 0x4b, /* data */
1037 0x81, 0x42, 0x30, 0x00, /* opcode: LOAD PCR3, v0 */
1038 0x81, 0x42, 0x40, 0x00, /* opcode: LOAD PCR4, v0 */
1039};
1040
Dirk Eibach762d3df2013-06-26 15:55:17 +02001041static int second_stage_init(void)
1042{
1043 static const char mac_suffix[] = ".mac";
1044 bool did_first_stage_run = true;
1045 int result = 0;
1046 char *cptr, *mmcdev = NULL;
1047 struct key_program *hmac_blob = NULL;
1048 const char *image_path = "/ccdm.itb";
1049 char *mac_path = NULL;
1050 ulong image_addr;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001051 loff_t image_size;
Simon Glass8ceca1d2018-11-18 14:22:27 -07001052 struct udevice *tpm;
Dirk Eibach762d3df2013-06-26 15:55:17 +02001053 uint32_t err;
Simon Glass8ceca1d2018-11-18 14:22:27 -07001054 int ret;
Dirk Eibach762d3df2013-06-26 15:55:17 +02001055
1056 printf("CCDM S2\n");
Simon Glass8ceca1d2018-11-18 14:22:27 -07001057 ret = get_tpm(&tpm);
1058 if (ret || tpm_init(tpm))
Dirk Eibach762d3df2013-06-26 15:55:17 +02001059 return 1;
Simon Glass8ceca1d2018-11-18 14:22:27 -07001060 err = tpm_startup(tpm, TPM_ST_CLEAR);
Dirk Eibach762d3df2013-06-26 15:55:17 +02001061 if (err != TPM_INVALID_POSTINIT)
1062 did_first_stage_run = false;
1063
1064#ifdef CCDM_AUTO_FIRST_STAGE
Simon Glass8ceca1d2018-11-18 14:22:27 -07001065 if (!did_first_stage_run && first_stage_actions(tpm))
Dirk Eibach762d3df2013-06-26 15:55:17 +02001066 goto failure;
1067#else
1068 if (!did_first_stage_run)
1069 goto failure;
1070#endif
1071
Simon Glass8ceca1d2018-11-18 14:22:27 -07001072 if (hre_run_program(tpm, prg_stage2_prepare,
1073 sizeof(prg_stage2_prepare)))
Dirk Eibach762d3df2013-06-26 15:55:17 +02001074 goto failure;
1075
1076 /* run "prepboot" from env to get "mmcdev" set */
Simon Glass64b723f2017-08-03 12:22:12 -06001077 cptr = env_get("prepboot");
Dirk Eibach762d3df2013-06-26 15:55:17 +02001078 if (cptr && !run_command(cptr, 0))
Simon Glass64b723f2017-08-03 12:22:12 -06001079 mmcdev = env_get("mmcdev");
Dirk Eibach762d3df2013-06-26 15:55:17 +02001080 if (!mmcdev)
1081 goto failure;
1082
Simon Glass64b723f2017-08-03 12:22:12 -06001083 cptr = env_get("ramdiskimage");
Dirk Eibach762d3df2013-06-26 15:55:17 +02001084 if (cptr)
1085 image_path = cptr;
1086
1087 mac_path = malloc(strlen(image_path) + strlen(mac_suffix) + 1);
1088 if (mac_path == NULL)
1089 goto failure;
1090 strcpy(mac_path, image_path);
1091 strcat(mac_path, mac_suffix);
1092
1093 /* read image from mmcdev (ccdm.itb) */
1094 image_addr = (ulong)get_image_location();
1095 if (fs_set_blk_dev("mmc", mmcdev, FS_TYPE_EXT))
1096 goto failure;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001097 if (fs_read(image_path, image_addr, 0, 0, &image_size) < 0)
1098 goto failure;
Dirk Eibach762d3df2013-06-26 15:55:17 +02001099 if (image_size <= 0)
1100 goto failure;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001101 printf("CCDM image found on %s, %lld bytes\n", mmcdev, image_size);
Dirk Eibach762d3df2013-06-26 15:55:17 +02001102
1103 hmac_blob = load_key_chunk("mmc", mmcdev, FS_TYPE_EXT, mac_path);
1104 if (!hmac_blob) {
1105 puts("failed to load mac file\n");
1106 goto failure;
1107 }
1108 if (verify_program(hmac_blob)) {
1109 puts("corrupted mac file\n");
1110 goto failure;
1111 }
1112 if (check_hmac(hmac_blob, (u8 *)image_addr, image_size)) {
1113 puts("image integrity could not be verified\n");
1114 goto failure;
1115 }
1116 puts("CCDM image OK\n");
1117
Simon Glass8ceca1d2018-11-18 14:22:27 -07001118 hre_run_program(tpm, prg_stage2_success, sizeof(prg_stage2_success));
Dirk Eibach762d3df2013-06-26 15:55:17 +02001119
1120 goto end;
1121failure:
1122 result = 1;
Simon Glass8ceca1d2018-11-18 14:22:27 -07001123 hre_run_program(tpm, prg_stage_fail, sizeof(prg_stage_fail));
Dirk Eibach762d3df2013-06-26 15:55:17 +02001124end:
1125 if (hmac_blob)
1126 free(hmac_blob);
1127 if (mac_path)
1128 free(mac_path);
1129
1130 return result;
1131}
1132#endif
1133
1134int show_self_hash(void)
1135{
1136 struct h_reg *hash_ptr;
1137#ifdef CCDM_SECOND_STAGE
1138 struct h_reg hash;
1139
1140 hash_ptr = &hash;
1141 if (compute_self_hash(hash_ptr))
1142 return 1;
1143#else
1144 hash_ptr = &fix_hregs[FIX_HREG_SELF_HASH];
1145#endif
1146 puts("self hash: ");
1147 if (hash_ptr && hash_ptr->valid)
1148 print_buffer(0, hash_ptr->digest, 1, 20, 20);
1149 else
1150 puts("INVALID\n");
1151
1152 return 0;
1153}
1154
1155/**
1156 * @brief let the system hang.
1157 *
1158 * Called on error.
1159 * Will stop the boot process; display a message and signal the error condition
1160 * by blinking the "status" and the "finder" LED of the controller board.
1161 *
1162 * @note the develop version runs the blink cycle 2 times and then returns.
1163 * The release version never returns.
1164 */
1165static void ccdm_hang(void)
1166{
1167 static const u64 f0 = 0x0ba3bb8ba2e880; /* blink code "finder" LED */
1168 static const u64 s0 = 0x00f0f0f0f0f0f0; /* blink code "status" LED */
1169 u64 f, s;
1170 int i;
1171#ifdef CCDM_DEVELOP
1172 int j;
1173#endif
1174
Dirk Eibache674bf12014-07-03 09:28:16 +02001175 I2C_SET_BUS(I2C_SOC_0);
Dirk Eibach762d3df2013-06-26 15:55:17 +02001176 pca9698_direction_output(0x22, 0, 0); /* Finder */
1177 pca9698_direction_output(0x22, 4, 0); /* Status */
1178
1179 puts("### ERROR ### Please RESET the board ###\n");
1180 bootstage_error(BOOTSTAGE_ID_NEED_RESET);
1181#ifdef CCDM_DEVELOP
1182 puts("*** ERROR ******** THIS WOULD HANG ******** ERROR ***\n");
1183 puts("** but we continue since this is a DEVELOP version **\n");
1184 puts("*** ERROR ******** THIS WOULD HANG ******** ERROR ***\n");
1185 for (j = 2; j-- > 0;) {
1186 putc('#');
1187#else
1188 for (;;) {
1189#endif
1190 f = f0;
1191 s = s0;
1192 for (i = 54; i-- > 0;) {
1193 pca9698_set_value(0x22, 0, !(f & 1));
1194 pca9698_set_value(0x22, 4, (s & 1));
1195 f >>= 1;
1196 s >>= 1;
1197 mdelay(120);
1198 }
1199 }
1200 puts("\ncontinue...\n");
1201}
1202
1203int startup_ccdm_id_module(void)
1204{
1205 int result = 0;
1206 unsigned int orig_i2c_bus;
1207
Dirk Eibache674bf12014-07-03 09:28:16 +02001208 orig_i2c_bus = i2c_get_bus_num();
1209 i2c_set_bus_num(I2C_SOC_1);
Dirk Eibach762d3df2013-06-26 15:55:17 +02001210
1211 /* goto end; */
1212
1213#ifdef CCDM_DEVELOP
1214 show_self_hash();
1215#endif
1216#ifdef CCDM_FIRST_STAGE
1217 result = first_stage_init();
1218 if (result) {
1219 puts("1st stage init failed\n");
1220 goto failure;
1221 }
1222#endif
1223#ifdef CCDM_SECOND_STAGE
1224 result = second_stage_init();
1225 if (result) {
1226 puts("2nd stage init failed\n");
1227 goto failure;
1228 }
1229#endif
1230
1231 goto end;
1232failure:
1233 result = 1;
1234end:
Dirk Eibache674bf12014-07-03 09:28:16 +02001235 i2c_set_bus_num(orig_i2c_bus);
Dirk Eibach762d3df2013-06-26 15:55:17 +02001236 if (result)
1237 ccdm_hang();
1238
1239 return result;
1240}