blob: d6798bd33829bc9fb7a4b9e142ac8517d9420e20 [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 Glass8ceca1d2018-11-18 14:22:27 -070014#include <dm.h>
Simon Glass6eaea252019-08-01 09:46:48 -060015#include <env.h>
Dirk Eibach762d3df2013-06-26 15:55:17 +020016#include <malloc.h>
17#include <fs.h>
18#include <i2c.h>
19#include <mmc.h>
Miquel Raynal4c6759e2018-05-15 11:57:06 +020020#include <tpm-v1.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070021#include <u-boot/crc.h>
Jeroen Hofsteebfe88fe2014-06-12 22:27:12 +020022#include <u-boot/sha1.h>
Dirk Eibach762d3df2013-06-26 15:55:17 +020023#include <asm/byteorder.h>
24#include <asm/unaligned.h>
25#include <pca9698.h>
26
27#undef CCDM_FIRST_STAGE
28#undef CCDM_SECOND_STAGE
29#undef CCDM_AUTO_FIRST_STAGE
30
31#ifdef CONFIG_DEVELOP
32#define CCDM_DEVELOP
33#endif
34
35#ifdef CONFIG_TRAILBLAZER
36#define CCDM_FIRST_STAGE
37#undef CCDM_SECOND_STAGE
38#else
39#undef CCDM_FIRST_STAGE
40#define CCDM_SECOND_STAGE
41#endif
42
43#if defined(CCDM_DEVELOP) && defined(CCDM_SECOND_STAGE) && \
44 !defined(CCCM_FIRST_STAGE)
45#define CCDM_AUTO_FIRST_STAGE
46#endif
47
Dirk Eibach762d3df2013-06-26 15:55:17 +020048/* CCDM specific contants */
49enum {
50 /* NV indices */
51 NV_COMMON_DATA_INDEX = 0x40000001,
52 /* magics for key blob chains */
53 MAGIC_KEY_PROGRAM = 0x68726500,
54 MAGIC_HMAC = 0x68616300,
55 MAGIC_END_OF_CHAIN = 0x00000000,
56 /* sizes */
57 NV_COMMON_DATA_MIN_SIZE = 3 * sizeof(uint64_t) + 2 * sizeof(uint16_t),
58};
59
60/* other constants */
61enum {
62 ESDHC_BOOT_IMAGE_SIG_OFS = 0x40,
63 ESDHC_BOOT_IMAGE_SIZE_OFS = 0x48,
64 ESDHC_BOOT_IMAGE_ADDR_OFS = 0x50,
65 ESDHC_BOOT_IMAGE_TARGET_OFS = 0x58,
66 ESDHC_BOOT_IMAGE_ENTRY_OFS = 0x60,
67};
68
Dirk Eibache674bf12014-07-03 09:28:16 +020069enum {
70 I2C_SOC_0 = 0,
71 I2C_SOC_1 = 1,
72};
73
Dirk Eibach762d3df2013-06-26 15:55:17 +020074struct key_program {
75 uint32_t magic;
76 uint32_t code_crc;
77 uint32_t code_size;
78 uint8_t code[];
79};
80
81struct h_reg {
82 bool valid;
83 uint8_t digest[20];
84};
85
86
87enum access_mode {
88 HREG_NONE = 0,
89 HREG_RD = 1,
90 HREG_WR = 2,
91 HREG_RDWR = 3,
92};
93
94/* register constants */
95enum {
96 FIX_HREG_DEVICE_ID_HASH = 0,
97 FIX_HREG_SELF_HASH = 1,
98 FIX_HREG_STAGE2_HASH = 2,
99 FIX_HREG_VENDOR = 3,
100 COUNT_FIX_HREGS
101};
102
103
104/* hre opcodes */
105enum {
106 /* opcodes w/o data */
107 HRE_NOP = 0x00,
108 HRE_SYNC = HRE_NOP,
109 HRE_CHECK0 = 0x01,
110 /* opcodes w/o data, w/ sync dst */
111 /* opcodes w/ data */
112 HRE_LOAD = 0x81,
113 /* opcodes w/data, w/sync dst */
114 HRE_XOR = 0xC1,
115 HRE_AND = 0xC2,
116 HRE_OR = 0xC3,
117 HRE_EXTEND = 0xC4,
118 HRE_LOADKEY = 0xC5,
119};
120
121/* hre errors */
122enum {
123 HRE_E_OK = 0,
124 HRE_E_TPM_FAILURE,
125 HRE_E_INVALID_HREG,
126};
127
128static uint64_t device_id;
129static uint64_t device_cl;
130static uint64_t device_type;
131
132static uint32_t platform_key_handle;
133
134static void(*bl2_entry)(void);
135
136static struct h_reg pcr_hregs[24];
137static struct h_reg fix_hregs[COUNT_FIX_HREGS];
138static struct h_reg var_hregs[8];
139static uint32_t hre_tpm_err;
140static int hre_err = HRE_E_OK;
141
142#define IS_PCR_HREG(spec) ((spec) & 0x20)
143#define IS_FIX_HREG(spec) (((spec) & 0x38) == 0x08)
144#define IS_VAR_HREG(spec) (((spec) & 0x38) == 0x10)
145#define HREG_IDX(spec) ((spec) & (IS_PCR_HREG(spec) ? 0x1f : 0x7))
146
Simon Glass8ceca1d2018-11-18 14:22:27 -0700147static int get_tpm(struct udevice **devp)
148{
149 int rc;
150
151 rc = uclass_first_device_err(UCLASS_TPM, devp);
152 if (rc) {
153 printf("Could not find TPM (ret=%d)\n", rc);
154 return CMD_RET_FAILURE;
155 }
156
157 return 0;
158}
159
Dirk Eibach762d3df2013-06-26 15:55:17 +0200160static const uint8_t vendor[] = "Guntermann & Drunck";
161
Dirk Eibach762d3df2013-06-26 15:55:17 +0200162/**
163 * @brief read a bunch of data from MMC into memory.
164 *
165 * @param mmc pointer to the mmc structure to use.
166 * @param src offset where the data starts on MMC/SD device (in bytes).
167 * @param dst pointer to the location where the read data should be stored.
168 * @param size number of bytes to read from the MMC/SD device.
169 * @return number of bytes read or -1 on error.
170 */
171static int ccdm_mmc_read(struct mmc *mmc, u64 src, u8 *dst, int size)
172{
173 int result = 0;
174 u32 blk_len, ofs;
175 ulong block_no, n, cnt;
176 u8 *tmp_buf = NULL;
177
178 if (size <= 0)
179 goto end;
180
181 blk_len = mmc->read_bl_len;
182 tmp_buf = malloc(blk_len);
183 if (!tmp_buf)
184 goto failure;
185 block_no = src / blk_len;
186 ofs = src % blk_len;
187
188 if (ofs) {
Stephen Warrene73f2962015-12-07 11:38:48 -0700189 n = mmc->block_dev.block_read(&mmc->block_dev, block_no++, 1,
Dirk Eibach762d3df2013-06-26 15:55:17 +0200190 tmp_buf);
191 if (!n)
192 goto failure;
Masahiro Yamadadb204642014-11-07 03:03:31 +0900193 result = min(size, (int)(blk_len - ofs));
Dirk Eibach762d3df2013-06-26 15:55:17 +0200194 memcpy(dst, tmp_buf + ofs, result);
195 dst += result;
196 size -= result;
197 }
198 cnt = size / blk_len;
199 if (cnt) {
Stephen Warrene73f2962015-12-07 11:38:48 -0700200 n = mmc->block_dev.block_read(&mmc->block_dev, block_no, cnt,
Dirk Eibach762d3df2013-06-26 15:55:17 +0200201 dst);
202 if (n != cnt)
203 goto failure;
204 size -= cnt * blk_len;
205 result += cnt * blk_len;
206 dst += cnt * blk_len;
207 block_no += cnt;
208 }
209 if (size) {
Stephen Warrene73f2962015-12-07 11:38:48 -0700210 n = mmc->block_dev.block_read(&mmc->block_dev, block_no++, 1,
Dirk Eibach762d3df2013-06-26 15:55:17 +0200211 tmp_buf);
212 if (!n)
213 goto failure;
214 memcpy(dst, tmp_buf, size);
215 result += size;
216 }
217 goto end;
218failure:
219 result = -1;
220end:
221 if (tmp_buf)
222 free(tmp_buf);
223 return result;
224}
225
226/**
227 * @brief returns a location where the 2nd stage bootloader can be(/ is) placed.
228 *
229 * @return pointer to the location for/of the 2nd stage bootloader
230 */
231static u8 *get_2nd_stage_bl_location(ulong target_addr)
232{
233 ulong addr;
234#ifdef CCDM_SECOND_STAGE
Simon Glass22c34c22017-08-03 12:22:13 -0600235 addr = env_get_ulong("loadaddr", 16, CONFIG_LOADADDR);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200236#else
237 addr = target_addr;
238#endif
239 return (u8 *)(addr);
240}
241
242
243#ifdef CCDM_SECOND_STAGE
244/**
245 * @brief returns a location where the image can be(/ is) placed.
246 *
247 * @return pointer to the location for/of the image
248 */
249static u8 *get_image_location(void)
250{
251 ulong addr;
252 /* TODO use other area? */
Simon Glass22c34c22017-08-03 12:22:13 -0600253 addr = env_get_ulong("loadaddr", 16, CONFIG_LOADADDR);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200254 return (u8 *)(addr);
255}
256#endif
257
258/**
259 * @brief get the size of a given (TPM) NV area
260 * @param index NV index of the area to get size for
261 * @param size pointer to the size
262 * @return 0 on success, != 0 on error
263 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700264static int get_tpm_nv_size(struct udevice *tpm, uint32_t index, uint32_t *size)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200265{
266 uint32_t err;
267 uint8_t info[72];
268 uint8_t *ptr;
269 uint16_t v16;
270
Simon Glass8ceca1d2018-11-18 14:22:27 -0700271 err = tpm_get_capability(tpm, TPM_CAP_NV_INDEX, index,
272 info, sizeof(info));
Dirk Eibach762d3df2013-06-26 15:55:17 +0200273 if (err) {
274 printf("tpm_get_capability(CAP_NV_INDEX, %08x) failed: %u\n",
275 index, err);
276 return 1;
277 }
278
279 /* skip tag and nvIndex */
280 ptr = info + 6;
281 /* skip 2 pcr info fields */
282 v16 = get_unaligned_be16(ptr);
283 ptr += 2 + v16 + 1 + 20;
284 v16 = get_unaligned_be16(ptr);
285 ptr += 2 + v16 + 1 + 20;
286 /* skip permission and flags */
287 ptr += 6 + 3;
288
289 *size = get_unaligned_be32(ptr);
290 return 0;
291}
292
293/**
294 * @brief search for a key by usage auth and pub key hash.
295 * @param auth usage auth of the key to search for
296 * @param pubkey_digest (SHA1) hash of the pub key structure of the key
297 * @param[out] handle the handle of the key iff found
298 * @return 0 if key was found in TPM; != 0 if not.
299 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700300static int find_key(struct udevice *tpm, const uint8_t auth[20],
301 const uint8_t pubkey_digest[20], uint32_t *handle)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200302{
303 uint16_t key_count;
304 uint32_t key_handles[10];
305 uint8_t buf[288];
306 uint8_t *ptr;
307 uint32_t err;
308 uint8_t digest[20];
309 size_t buf_len;
310 unsigned int i;
311
312 /* fetch list of already loaded keys in the TPM */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700313 err = tpm_get_capability(tpm, TPM_CAP_HANDLE, TPM_RT_KEY, buf,
314 sizeof(buf));
Dirk Eibach762d3df2013-06-26 15:55:17 +0200315 if (err)
316 return -1;
317 key_count = get_unaligned_be16(buf);
318 ptr = buf + 2;
319 for (i = 0; i < key_count; ++i, ptr += 4)
320 key_handles[i] = get_unaligned_be32(ptr);
321
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);
Simon Glass8ceca1d2018-11-18 14:22:27 -0700325 err = tpm_get_pub_key_oiap(tpm, key_handles[i], auth, buf,
326 &buf_len);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200327 if (err && err != TPM_AUTHFAIL)
328 return -1;
329 if (err)
330 continue;
331 sha1_csum(buf, buf_len, digest);
332 if (!memcmp(digest, pubkey_digest, 20)) {
333 *handle = key_handles[i];
334 return 0;
335 }
336 }
337 return 1;
338}
339
340/**
341 * @brief read CCDM common data from TPM NV
342 * @return 0 if CCDM common data was found and read, !=0 if something failed.
343 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700344static int read_common_data(struct udevice *tpm)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200345{
346 uint32_t size;
347 uint32_t err;
348 uint8_t buf[256];
349 sha1_context ctx;
350
Simon Glass8ceca1d2018-11-18 14:22:27 -0700351 if (get_tpm_nv_size(tpm, NV_COMMON_DATA_INDEX, &size) ||
Dirk Eibach762d3df2013-06-26 15:55:17 +0200352 size < NV_COMMON_DATA_MIN_SIZE)
353 return 1;
Simon Glass8ceca1d2018-11-18 14:22:27 -0700354 err = tpm_nv_read_value(tpm, NV_COMMON_DATA_INDEX,
355 buf, min(sizeof(buf), size));
Dirk Eibach762d3df2013-06-26 15:55:17 +0200356 if (err) {
357 printf("tpm_nv_read_value() failed: %u\n", err);
358 return 1;
359 }
360
361 device_id = get_unaligned_be64(buf);
362 device_cl = get_unaligned_be64(buf + 8);
363 device_type = get_unaligned_be64(buf + 16);
364
365 sha1_starts(&ctx);
366 sha1_update(&ctx, buf, 24);
367 sha1_finish(&ctx, fix_hregs[FIX_HREG_DEVICE_ID_HASH].digest);
368 fix_hregs[FIX_HREG_DEVICE_ID_HASH].valid = true;
369
370 platform_key_handle = get_unaligned_be32(buf + 24);
371
372 return 0;
373}
374
375/**
376 * @brief compute hash of bootloader itself.
377 * @param[out] dst hash register where the hash should be stored
378 * @return 0 on success, != 0 on failure.
379 *
380 * @note MUST be called at a time where the boot loader is accessible at the
381 * configured location (; so take care when code is reallocated).
382 */
383static int compute_self_hash(struct h_reg *dst)
384{
385 sha1_csum((const uint8_t *)CONFIG_SYS_MONITOR_BASE,
386 CONFIG_SYS_MONITOR_LEN, dst->digest);
387 dst->valid = true;
388 return 0;
389}
390
391int ccdm_compute_self_hash(void)
392{
393 if (!fix_hregs[FIX_HREG_SELF_HASH].valid)
394 compute_self_hash(&fix_hregs[FIX_HREG_SELF_HASH]);
395 return 0;
396}
397
398/**
399 * @brief compute the hash of the 2nd stage boot loader (on SD card)
400 * @param[out] dst hash register to store the computed hash
401 * @return 0 on success, != 0 on failure
402 *
403 * Determines the size and location of the 2nd stage boot loader on SD card,
404 * loads the 2nd stage boot loader and computes the (SHA1) hash value.
405 * Within the 1st stage boot loader, the 2nd stage boot loader is loaded at
406 * the desired memory location and the variable @a bl2_entry is set.
407 *
408 * @note This sets the variable @a bl2_entry to the entry point when the
409 * 2nd stage boot loader is loaded at its configured memory location.
410 */
411static int compute_second_stage_hash(struct h_reg *dst)
412{
413 int result = 0;
414 u32 code_len, code_offset, target_addr, exec_entry;
415 struct mmc *mmc;
416 u8 *load_addr = NULL;
417 u8 buf[128];
418
419 mmc = find_mmc_device(0);
420 if (!mmc)
421 goto failure;
422 mmc_init(mmc);
423
424 if (ccdm_mmc_read(mmc, 0, buf, sizeof(buf)) < 0)
425 goto failure;
426
427 code_offset = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ADDR_OFS);
428 code_len = *(u32 *)(buf + ESDHC_BOOT_IMAGE_SIZE_OFS);
429 target_addr = *(u32 *)(buf + ESDHC_BOOT_IMAGE_TARGET_OFS);
430 exec_entry = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ENTRY_OFS);
431
432 load_addr = get_2nd_stage_bl_location(target_addr);
433 if (load_addr == (u8 *)target_addr)
434 bl2_entry = (void(*)(void))exec_entry;
435
436 if (ccdm_mmc_read(mmc, code_offset, load_addr, code_len) < 0)
437 goto failure;
438
439 sha1_csum(load_addr, code_len, dst->digest);
440 dst->valid = true;
441
442 goto end;
443failure:
444 result = 1;
445 bl2_entry = NULL;
446end:
447 return result;
448}
449
450/**
451 * @brief get pointer to hash register by specification
452 * @param spec specification of a hash register
453 * @return pointer to hash register or NULL if @a spec does not qualify a
454 * valid hash register; NULL else.
455 */
456static struct h_reg *get_hreg(uint8_t spec)
457{
458 uint8_t idx;
459
460 idx = HREG_IDX(spec);
461 if (IS_FIX_HREG(spec)) {
462 if (idx < ARRAY_SIZE(fix_hregs))
463 return fix_hregs + idx;
464 hre_err = HRE_E_INVALID_HREG;
465 } else if (IS_PCR_HREG(spec)) {
466 if (idx < ARRAY_SIZE(pcr_hregs))
467 return pcr_hregs + idx;
468 hre_err = HRE_E_INVALID_HREG;
469 } else if (IS_VAR_HREG(spec)) {
470 if (idx < ARRAY_SIZE(var_hregs))
471 return var_hregs + idx;
472 hre_err = HRE_E_INVALID_HREG;
473 }
474 return NULL;
475}
476
477/**
478 * @brief get pointer of a hash register by specification and usage.
479 * @param spec specification of a hash register
480 * @param mode access mode (read or write or read/write)
481 * @return pointer to hash register if found and valid; NULL else.
482 *
483 * This func uses @a get_reg() to determine the hash register for a given spec.
484 * If a register is found it is validated according to the desired access mode.
485 * The value of automatic registers (PCR register and fixed registers) is
486 * loaded or computed on read access.
487 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700488static struct h_reg *access_hreg(struct udevice *tpm, uint8_t spec,
489 enum access_mode mode)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200490{
491 struct h_reg *result;
492
493 result = get_hreg(spec);
494 if (!result)
495 return NULL;
496
497 if (mode & HREG_WR) {
498 if (IS_FIX_HREG(spec)) {
499 hre_err = HRE_E_INVALID_HREG;
500 return NULL;
501 }
502 }
503 if (mode & HREG_RD) {
504 if (!result->valid) {
505 if (IS_PCR_HREG(spec)) {
Simon Glass8ceca1d2018-11-18 14:22:27 -0700506 hre_tpm_err = tpm_pcr_read(tpm, HREG_IDX(spec),
Dirk Eibach762d3df2013-06-26 15:55:17 +0200507 result->digest, 20);
508 result->valid = (hre_tpm_err == TPM_SUCCESS);
509 } else if (IS_FIX_HREG(spec)) {
510 switch (HREG_IDX(spec)) {
511 case FIX_HREG_DEVICE_ID_HASH:
Simon Glass8ceca1d2018-11-18 14:22:27 -0700512 read_common_data(tpm);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200513 break;
514 case FIX_HREG_SELF_HASH:
515 ccdm_compute_self_hash();
516 break;
517 case FIX_HREG_STAGE2_HASH:
518 compute_second_stage_hash(result);
519 break;
520 case FIX_HREG_VENDOR:
521 memcpy(result->digest, vendor, 20);
522 result->valid = true;
523 break;
524 }
525 } else {
526 result->valid = true;
527 }
528 }
529 if (!result->valid) {
530 hre_err = HRE_E_INVALID_HREG;
531 return NULL;
532 }
533 }
534
535 return result;
536}
537
538static void *compute_and(void *_dst, const void *_src, size_t n)
539{
540 uint8_t *dst = _dst;
541 const uint8_t *src = _src;
542 size_t i;
543
544 for (i = n; i-- > 0; )
545 *dst++ &= *src++;
546
547 return _dst;
548}
549
550static void *compute_or(void *_dst, const void *_src, size_t n)
551{
552 uint8_t *dst = _dst;
553 const uint8_t *src = _src;
554 size_t i;
555
556 for (i = n; i-- > 0; )
557 *dst++ |= *src++;
558
559 return _dst;
560}
561
562static void *compute_xor(void *_dst, const void *_src, size_t n)
563{
564 uint8_t *dst = _dst;
565 const uint8_t *src = _src;
566 size_t i;
567
568 for (i = n; i-- > 0; )
569 *dst++ ^= *src++;
570
571 return _dst;
572}
573
574static void *compute_extend(void *_dst, const void *_src, size_t n)
575{
576 uint8_t digest[20];
577 sha1_context ctx;
578
579 sha1_starts(&ctx);
580 sha1_update(&ctx, _dst, n);
581 sha1_update(&ctx, _src, n);
582 sha1_finish(&ctx, digest);
583 memcpy(_dst, digest, min(n, sizeof(digest)));
584
585 return _dst;
586}
587
Simon Glass8ceca1d2018-11-18 14:22:27 -0700588static int hre_op_loadkey(struct udevice *tpm, struct h_reg *src_reg,
589 struct h_reg *dst_reg, const void *key,
590 size_t key_size)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200591{
592 uint32_t parent_handle;
593 uint32_t key_handle;
594
595 if (!src_reg || !dst_reg || !src_reg->valid || !dst_reg->valid)
596 return -1;
Simon Glass8ceca1d2018-11-18 14:22:27 -0700597 if (find_key(tpm, src_reg->digest, dst_reg->digest, &parent_handle))
Dirk Eibach762d3df2013-06-26 15:55:17 +0200598 return -1;
Simon Glass8ceca1d2018-11-18 14:22:27 -0700599 hre_tpm_err = tpm_load_key2_oiap(tpm, parent_handle, key, key_size,
600 src_reg->digest, &key_handle);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200601 if (hre_tpm_err) {
602 hre_err = HRE_E_TPM_FAILURE;
603 return -1;
604 }
605 /* TODO remember key handle somehow? */
606
607 return 0;
608}
609
610/**
611 * @brief executes the next opcode on the hash register engine.
612 * @param[in,out] ip pointer to the opcode (instruction pointer)
613 * @param[in,out] code_size (remaining) size of the code
614 * @return new instruction pointer on success, NULL on error.
615 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700616static const uint8_t *hre_execute_op(struct udevice *tpm, const uint8_t **ip,
617 size_t *code_size)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200618{
619 bool dst_modified = false;
620 uint32_t ins;
621 uint8_t opcode;
622 uint8_t src_spec;
623 uint8_t dst_spec;
624 uint16_t data_size;
625 struct h_reg *src_reg, *dst_reg;
626 uint8_t buf[20];
627 const uint8_t *src_buf, *data;
628 uint8_t *ptr;
629 int i;
630 void * (*bin_func)(void *, const void *, size_t);
631
632 if (*code_size < 4)
633 return NULL;
634
635 ins = get_unaligned_be32(*ip);
636 opcode = **ip;
637 data = *ip + 4;
638 src_spec = (ins >> 18) & 0x3f;
639 dst_spec = (ins >> 12) & 0x3f;
640 data_size = (ins & 0x7ff);
641
642 debug("HRE: ins=%08x (op=%02x, s=%02x, d=%02x, L=%d)\n", ins,
643 opcode, src_spec, dst_spec, data_size);
644
645 if ((opcode & 0x80) && (data_size + 4) > *code_size)
646 return NULL;
647
Simon Glass8ceca1d2018-11-18 14:22:27 -0700648 src_reg = access_hreg(tpm, src_spec, HREG_RD);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200649 if (hre_err || hre_tpm_err)
650 return NULL;
Simon Glass8ceca1d2018-11-18 14:22:27 -0700651 dst_reg = access_hreg(tpm, dst_spec,
652 (opcode & 0x40) ? HREG_RDWR : HREG_WR);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200653 if (hre_err || hre_tpm_err)
654 return NULL;
655
656 switch (opcode) {
657 case HRE_NOP:
658 goto end;
659 case HRE_CHECK0:
660 if (src_reg) {
661 for (i = 0; i < 20; ++i) {
662 if (src_reg->digest[i])
663 return NULL;
664 }
665 }
666 break;
667 case HRE_LOAD:
668 bin_func = memcpy;
669 goto do_bin_func;
670 case HRE_XOR:
671 bin_func = compute_xor;
672 goto do_bin_func;
673 case HRE_AND:
674 bin_func = compute_and;
675 goto do_bin_func;
676 case HRE_OR:
677 bin_func = compute_or;
678 goto do_bin_func;
679 case HRE_EXTEND:
680 bin_func = compute_extend;
681do_bin_func:
682 if (!dst_reg)
683 return NULL;
684 if (src_reg) {
685 src_buf = src_reg->digest;
686 } else {
687 if (!data_size) {
688 memset(buf, 0, 20);
689 src_buf = buf;
690 } else if (data_size == 1) {
691 memset(buf, *data, 20);
692 src_buf = buf;
693 } else if (data_size >= 20) {
694 src_buf = data;
695 } else {
696 src_buf = buf;
697 for (ptr = (uint8_t *)src_buf, i = 20; i > 0;
698 i -= data_size, ptr += data_size)
Masahiro Yamadadb204642014-11-07 03:03:31 +0900699 memcpy(ptr, data,
700 min_t(size_t, i, data_size));
Dirk Eibach762d3df2013-06-26 15:55:17 +0200701 }
702 }
703 bin_func(dst_reg->digest, src_buf, 20);
704 dst_reg->valid = true;
705 dst_modified = true;
706 break;
707 case HRE_LOADKEY:
Simon Glass8ceca1d2018-11-18 14:22:27 -0700708 if (hre_op_loadkey(tpm, src_reg, dst_reg, data, data_size))
Dirk Eibach762d3df2013-06-26 15:55:17 +0200709 return NULL;
710 break;
711 default:
712 return NULL;
713 }
714
715 if (dst_reg && dst_modified && IS_PCR_HREG(dst_spec)) {
Simon Glass8ceca1d2018-11-18 14:22:27 -0700716 hre_tpm_err = tpm_extend(tpm, HREG_IDX(dst_spec),
717 dst_reg->digest, dst_reg->digest);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200718 if (hre_tpm_err) {
719 hre_err = HRE_E_TPM_FAILURE;
720 return NULL;
721 }
722 }
723end:
724 *ip += 4;
725 *code_size -= 4;
726 if (opcode & 0x80) {
727 *ip += data_size;
728 *code_size -= data_size;
729 }
730
731 return *ip;
732}
733
734/**
735 * @brief runs a program on the hash register engine.
736 * @param code pointer to the (HRE) code.
737 * @param code_size size of the code (in bytes).
738 * @return 0 on success, != 0 on failure.
739 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700740static int hre_run_program(struct udevice *tpm, const uint8_t *code,
741 size_t code_size)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200742{
743 size_t code_left;
744 const uint8_t *ip = code;
745
746 code_left = code_size;
747 hre_tpm_err = 0;
748 hre_err = HRE_E_OK;
749 while (code_left > 0)
Simon Glass8ceca1d2018-11-18 14:22:27 -0700750 if (!hre_execute_op(tpm, &ip, &code_left))
Dirk Eibach762d3df2013-06-26 15:55:17 +0200751 return -1;
752
753 return hre_err;
754}
755
756static int check_hmac(struct key_program *hmac,
757 const uint8_t *data, size_t data_size)
758{
759 uint8_t key[20], computed_hmac[20];
760 uint32_t type;
761
762 type = get_unaligned_be32(hmac->code);
763 if (type != 0)
764 return 1;
765 memset(key, 0, sizeof(key));
766 compute_extend(key, pcr_hregs[1].digest, 20);
767 compute_extend(key, pcr_hregs[2].digest, 20);
768 compute_extend(key, pcr_hregs[3].digest, 20);
769 compute_extend(key, pcr_hregs[4].digest, 20);
770
771 sha1_hmac(key, sizeof(key), data, data_size, computed_hmac);
772
773 return memcmp(computed_hmac, hmac->code + 4, 20);
774}
775
776static int verify_program(struct key_program *prg)
777{
778 uint32_t crc;
779 crc = crc32(0, prg->code, prg->code_size);
780
781 if (crc != prg->code_crc) {
782 printf("HRC crc mismatch: %08x != %08x\n",
783 crc, prg->code_crc);
784 return 1;
785 }
786 return 0;
787}
788
789#if defined(CCDM_FIRST_STAGE) || (defined CCDM_AUTO_FIRST_STAGE)
790static struct key_program *load_sd_key_program(void)
791{
792 u32 code_len, code_offset;
793 struct mmc *mmc;
794 u8 buf[128];
795 struct key_program *result = NULL, *hmac = NULL;
796 struct key_program header;
797
798 mmc = find_mmc_device(0);
799 if (!mmc)
800 return NULL;
801 mmc_init(mmc);
802
803 if (ccdm_mmc_read(mmc, 0, buf, sizeof(buf)) <= 0)
804 goto failure;
805
806 code_offset = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ADDR_OFS);
807 code_len = *(u32 *)(buf + ESDHC_BOOT_IMAGE_SIZE_OFS);
808
809 code_offset += code_len;
810 /* TODO: the following needs to be the size of the 2nd stage env */
811 code_offset += CONFIG_ENV_SIZE;
812
813 if (ccdm_mmc_read(mmc, code_offset, buf, 4*3) < 0)
814 goto failure;
815
816 header.magic = get_unaligned_be32(buf);
817 header.code_crc = get_unaligned_be32(buf + 4);
818 header.code_size = get_unaligned_be32(buf + 8);
819
820 if (header.magic != MAGIC_KEY_PROGRAM)
821 goto failure;
822
823 result = malloc(sizeof(struct key_program) + header.code_size);
824 if (!result)
825 goto failure;
826 *result = header;
827
828 printf("load key program chunk from SD card (%u bytes) ",
829 header.code_size);
830 code_offset += 12;
831 if (ccdm_mmc_read(mmc, code_offset, result->code, header.code_size)
832 < 0)
833 goto failure;
834 code_offset += header.code_size;
835 puts("\n");
836
837 if (verify_program(result))
838 goto failure;
839
840 if (ccdm_mmc_read(mmc, code_offset, buf, 4*3) < 0)
841 goto failure;
842
843 header.magic = get_unaligned_be32(buf);
844 header.code_crc = get_unaligned_be32(buf + 4);
845 header.code_size = get_unaligned_be32(buf + 8);
846
847 if (header.magic == MAGIC_HMAC) {
848 puts("check integrity\n");
849 hmac = malloc(sizeof(struct key_program) + header.code_size);
850 if (!hmac)
851 goto failure;
852 *hmac = header;
853 code_offset += 12;
854 if (ccdm_mmc_read(mmc, code_offset, hmac->code,
855 hmac->code_size) < 0)
856 goto failure;
857 if (verify_program(hmac))
858 goto failure;
859 if (check_hmac(hmac, result->code, result->code_size)) {
860 puts("key program integrity could not be verified\n");
861 goto failure;
862 }
863 puts("key program verified\n");
864 }
865
866 goto end;
867failure:
868 if (result)
869 free(result);
870 result = NULL;
871end:
872 if (hmac)
873 free(hmac);
874
875 return result;
876}
877#endif
878
879#ifdef CCDM_SECOND_STAGE
880/**
881 * @brief load a key program from file system.
882 * @param ifname interface of the file system
883 * @param dev_part_str device part of the file system
884 * @param fs_type tyep of the file system
885 * @param path path of the file to load.
886 * @return the loaded structure or NULL on failure.
887 */
888static struct key_program *load_key_chunk(const char *ifname,
889 const char *dev_part_str, int fs_type,
890 const char *path)
891{
892 struct key_program *result = NULL;
893 struct key_program header;
894 uint32_t crc;
895 uint8_t buf[12];
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800896 loff_t i;
Dirk Eibach762d3df2013-06-26 15:55:17 +0200897
898 if (fs_set_blk_dev(ifname, dev_part_str, fs_type))
899 goto failure;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800900 if (fs_read(path, (ulong)buf, 0, 12, &i) < 0)
901 goto failure;
Dirk Eibach762d3df2013-06-26 15:55:17 +0200902 if (i < 12)
903 goto failure;
904 header.magic = get_unaligned_be32(buf);
905 header.code_crc = get_unaligned_be32(buf + 4);
906 header.code_size = get_unaligned_be32(buf + 8);
907
908 if (header.magic != MAGIC_HMAC && header.magic != MAGIC_KEY_PROGRAM)
909 goto failure;
910
911 result = malloc(sizeof(struct key_program) + header.code_size);
912 if (!result)
913 goto failure;
914 if (fs_set_blk_dev(ifname, dev_part_str, fs_type))
915 goto failure;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800916 if (fs_read(path, (ulong)result, 0,
917 sizeof(struct key_program) + header.code_size, &i) < 0)
918 goto failure;
Dirk Eibach762d3df2013-06-26 15:55:17 +0200919 if (i <= 0)
920 goto failure;
921 *result = header;
922
923 crc = crc32(0, result->code, result->code_size);
924
925 if (crc != result->code_crc) {
926 printf("%s: HRC crc mismatch: %08x != %08x\n",
927 path, crc, result->code_crc);
928 goto failure;
929 }
930 goto end;
931failure:
932 if (result) {
933 free(result);
934 result = NULL;
935 }
936end:
937 return result;
938}
939#endif
940
941#if defined(CCDM_FIRST_STAGE) || (defined CCDM_AUTO_FIRST_STAGE)
Tom Rini910c7932017-06-16 13:06:26 -0400942static const uint8_t prg_stage1_prepare[] = {
943 0x00, 0x20, 0x00, 0x00, /* opcode: SYNC f0 */
944 0x00, 0x24, 0x00, 0x00, /* opcode: SYNC f1 */
945 0x01, 0x80, 0x00, 0x00, /* opcode: CHECK0 PCR0 */
946 0x81, 0x22, 0x00, 0x00, /* opcode: LOAD PCR0, f0 */
947 0x01, 0x84, 0x00, 0x00, /* opcode: CHECK0 PCR1 */
948 0x81, 0x26, 0x10, 0x00, /* opcode: LOAD PCR1, f1 */
949 0x01, 0x88, 0x00, 0x00, /* opcode: CHECK0 PCR2 */
950 0x81, 0x2a, 0x20, 0x00, /* opcode: LOAD PCR2, f2 */
951 0x01, 0x8c, 0x00, 0x00, /* opcode: CHECK0 PCR3 */
952 0x81, 0x2e, 0x30, 0x00, /* opcode: LOAD PCR3, f3 */
953};
954
Simon Glass8ceca1d2018-11-18 14:22:27 -0700955static int first_stage_actions(struct udevice *tpm)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200956{
957 int result = 0;
958 struct key_program *sd_prg = NULL;
959
960 puts("CCDM S1: start actions\n");
961#ifndef CCDM_SECOND_STAGE
Simon Glass8ceca1d2018-11-18 14:22:27 -0700962 if (tpm_continue_self_test(tpm))
Dirk Eibach762d3df2013-06-26 15:55:17 +0200963 goto failure;
964#else
Simon Glass8ceca1d2018-11-18 14:22:27 -0700965 tpm_continue_self_test(tpm);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200966#endif
967 mdelay(37);
968
Simon Glass8ceca1d2018-11-18 14:22:27 -0700969 if (hre_run_program(tpm, prg_stage1_prepare,
970 sizeof(prg_stage1_prepare)))
Dirk Eibach762d3df2013-06-26 15:55:17 +0200971 goto failure;
972
973 sd_prg = load_sd_key_program();
974 if (sd_prg) {
Simon Glass8ceca1d2018-11-18 14:22:27 -0700975 if (hre_run_program(tpm, sd_prg->code, sd_prg->code_size))
Dirk Eibach762d3df2013-06-26 15:55:17 +0200976 goto failure;
977 puts("SD code run successfully\n");
978 } else {
979 puts("no key program found on SD\n");
980 goto failure;
981 }
982 goto end;
983failure:
984 result = 1;
985end:
986 if (sd_prg)
987 free(sd_prg);
988 printf("CCDM S1: actions done (%d)\n", result);
989 return result;
990}
991#endif
992
993#ifdef CCDM_FIRST_STAGE
994static int first_stage_init(void)
995{
Simon Glass8ceca1d2018-11-18 14:22:27 -0700996 struct udevice *tpm;
997 int ret;
998
Dirk Eibach762d3df2013-06-26 15:55:17 +0200999 puts("CCDM S1\n");
Simon Glass8ceca1d2018-11-18 14:22:27 -07001000 ret = get_tpm(&tpm);
1001 if (ret || tpm_init(tpm) || tpm_startup(tpm, TPM_ST_CLEAR))
Dirk Eibach762d3df2013-06-26 15:55:17 +02001002 return 1;
Simon Glass8ceca1d2018-11-18 14:22:27 -07001003 ret = first_stage_actions(tpm);
Dirk Eibach762d3df2013-06-26 15:55:17 +02001004#ifndef CCDM_SECOND_STAGE
Simon Glass8ceca1d2018-11-18 14:22:27 -07001005 if (!ret) {
Dirk Eibach762d3df2013-06-26 15:55:17 +02001006 if (bl2_entry)
1007 (*bl2_entry)();
Simon Glass8ceca1d2018-11-18 14:22:27 -07001008 ret = 1;
Dirk Eibach762d3df2013-06-26 15:55:17 +02001009 }
1010#endif
Simon Glass8ceca1d2018-11-18 14:22:27 -07001011 return ret;
Dirk Eibach762d3df2013-06-26 15:55:17 +02001012}
1013#endif
1014
1015#ifdef CCDM_SECOND_STAGE
Tom Rini09868d42017-05-08 22:14:26 -04001016static const uint8_t prg_stage2_prepare[] = {
1017 0x00, 0x80, 0x00, 0x00, /* opcode: SYNC PCR0 */
1018 0x00, 0x84, 0x00, 0x00, /* opcode: SYNC PCR1 */
1019 0x00, 0x88, 0x00, 0x00, /* opcode: SYNC PCR2 */
1020 0x00, 0x8c, 0x00, 0x00, /* opcode: SYNC PCR3 */
1021 0x00, 0x90, 0x00, 0x00, /* opcode: SYNC PCR4 */
1022};
1023
1024static const uint8_t prg_stage2_success[] = {
1025 0x81, 0x02, 0x40, 0x14, /* opcode: LOAD PCR4, #<20B data> */
1026 0x48, 0xfd, 0x95, 0x17, 0xe7, 0x54, 0x6b, 0x68, /* data */
1027 0x92, 0x31, 0x18, 0x05, 0xf8, 0x58, 0x58, 0x3c, /* data */
1028 0xe4, 0xd2, 0x81, 0xe0, /* data */
1029};
1030
1031static const uint8_t prg_stage_fail[] = {
1032 0x81, 0x01, 0x00, 0x14, /* opcode: LOAD v0, #<20B data> */
1033 0xc0, 0x32, 0xad, 0xc1, 0xff, 0x62, 0x9c, 0x9b, /* data */
1034 0x66, 0xf2, 0x27, 0x49, 0xad, 0x66, 0x7e, 0x6b, /* data */
1035 0xea, 0xdf, 0x14, 0x4b, /* data */
1036 0x81, 0x42, 0x30, 0x00, /* opcode: LOAD PCR3, v0 */
1037 0x81, 0x42, 0x40, 0x00, /* opcode: LOAD PCR4, v0 */
1038};
1039
Dirk Eibach762d3df2013-06-26 15:55:17 +02001040static int second_stage_init(void)
1041{
1042 static const char mac_suffix[] = ".mac";
1043 bool did_first_stage_run = true;
1044 int result = 0;
1045 char *cptr, *mmcdev = NULL;
1046 struct key_program *hmac_blob = NULL;
1047 const char *image_path = "/ccdm.itb";
1048 char *mac_path = NULL;
1049 ulong image_addr;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001050 loff_t image_size;
Simon Glass8ceca1d2018-11-18 14:22:27 -07001051 struct udevice *tpm;
Dirk Eibach762d3df2013-06-26 15:55:17 +02001052 uint32_t err;
Simon Glass8ceca1d2018-11-18 14:22:27 -07001053 int ret;
Dirk Eibach762d3df2013-06-26 15:55:17 +02001054
1055 printf("CCDM S2\n");
Simon Glass8ceca1d2018-11-18 14:22:27 -07001056 ret = get_tpm(&tpm);
1057 if (ret || tpm_init(tpm))
Dirk Eibach762d3df2013-06-26 15:55:17 +02001058 return 1;
Simon Glass8ceca1d2018-11-18 14:22:27 -07001059 err = tpm_startup(tpm, TPM_ST_CLEAR);
Dirk Eibach762d3df2013-06-26 15:55:17 +02001060 if (err != TPM_INVALID_POSTINIT)
1061 did_first_stage_run = false;
1062
1063#ifdef CCDM_AUTO_FIRST_STAGE
Simon Glass8ceca1d2018-11-18 14:22:27 -07001064 if (!did_first_stage_run && first_stage_actions(tpm))
Dirk Eibach762d3df2013-06-26 15:55:17 +02001065 goto failure;
1066#else
1067 if (!did_first_stage_run)
1068 goto failure;
1069#endif
1070
Simon Glass8ceca1d2018-11-18 14:22:27 -07001071 if (hre_run_program(tpm, prg_stage2_prepare,
1072 sizeof(prg_stage2_prepare)))
Dirk Eibach762d3df2013-06-26 15:55:17 +02001073 goto failure;
1074
1075 /* run "prepboot" from env to get "mmcdev" set */
Simon Glass64b723f2017-08-03 12:22:12 -06001076 cptr = env_get("prepboot");
Dirk Eibach762d3df2013-06-26 15:55:17 +02001077 if (cptr && !run_command(cptr, 0))
Simon Glass64b723f2017-08-03 12:22:12 -06001078 mmcdev = env_get("mmcdev");
Dirk Eibach762d3df2013-06-26 15:55:17 +02001079 if (!mmcdev)
1080 goto failure;
1081
Simon Glass64b723f2017-08-03 12:22:12 -06001082 cptr = env_get("ramdiskimage");
Dirk Eibach762d3df2013-06-26 15:55:17 +02001083 if (cptr)
1084 image_path = cptr;
1085
1086 mac_path = malloc(strlen(image_path) + strlen(mac_suffix) + 1);
1087 if (mac_path == NULL)
1088 goto failure;
1089 strcpy(mac_path, image_path);
1090 strcat(mac_path, mac_suffix);
1091
1092 /* read image from mmcdev (ccdm.itb) */
1093 image_addr = (ulong)get_image_location();
1094 if (fs_set_blk_dev("mmc", mmcdev, FS_TYPE_EXT))
1095 goto failure;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001096 if (fs_read(image_path, image_addr, 0, 0, &image_size) < 0)
1097 goto failure;
Dirk Eibach762d3df2013-06-26 15:55:17 +02001098 if (image_size <= 0)
1099 goto failure;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001100 printf("CCDM image found on %s, %lld bytes\n", mmcdev, image_size);
Dirk Eibach762d3df2013-06-26 15:55:17 +02001101
1102 hmac_blob = load_key_chunk("mmc", mmcdev, FS_TYPE_EXT, mac_path);
1103 if (!hmac_blob) {
1104 puts("failed to load mac file\n");
1105 goto failure;
1106 }
1107 if (verify_program(hmac_blob)) {
1108 puts("corrupted mac file\n");
1109 goto failure;
1110 }
1111 if (check_hmac(hmac_blob, (u8 *)image_addr, image_size)) {
1112 puts("image integrity could not be verified\n");
1113 goto failure;
1114 }
1115 puts("CCDM image OK\n");
1116
Simon Glass8ceca1d2018-11-18 14:22:27 -07001117 hre_run_program(tpm, prg_stage2_success, sizeof(prg_stage2_success));
Dirk Eibach762d3df2013-06-26 15:55:17 +02001118
1119 goto end;
1120failure:
1121 result = 1;
Simon Glass8ceca1d2018-11-18 14:22:27 -07001122 hre_run_program(tpm, prg_stage_fail, sizeof(prg_stage_fail));
Dirk Eibach762d3df2013-06-26 15:55:17 +02001123end:
1124 if (hmac_blob)
1125 free(hmac_blob);
1126 if (mac_path)
1127 free(mac_path);
1128
1129 return result;
1130}
1131#endif
1132
1133int show_self_hash(void)
1134{
1135 struct h_reg *hash_ptr;
1136#ifdef CCDM_SECOND_STAGE
1137 struct h_reg hash;
1138
1139 hash_ptr = &hash;
1140 if (compute_self_hash(hash_ptr))
1141 return 1;
1142#else
1143 hash_ptr = &fix_hregs[FIX_HREG_SELF_HASH];
1144#endif
1145 puts("self hash: ");
1146 if (hash_ptr && hash_ptr->valid)
1147 print_buffer(0, hash_ptr->digest, 1, 20, 20);
1148 else
1149 puts("INVALID\n");
1150
1151 return 0;
1152}
1153
1154/**
1155 * @brief let the system hang.
1156 *
1157 * Called on error.
1158 * Will stop the boot process; display a message and signal the error condition
1159 * by blinking the "status" and the "finder" LED of the controller board.
1160 *
1161 * @note the develop version runs the blink cycle 2 times and then returns.
1162 * The release version never returns.
1163 */
1164static void ccdm_hang(void)
1165{
1166 static const u64 f0 = 0x0ba3bb8ba2e880; /* blink code "finder" LED */
1167 static const u64 s0 = 0x00f0f0f0f0f0f0; /* blink code "status" LED */
1168 u64 f, s;
1169 int i;
1170#ifdef CCDM_DEVELOP
1171 int j;
1172#endif
1173
Dirk Eibache674bf12014-07-03 09:28:16 +02001174 I2C_SET_BUS(I2C_SOC_0);
Dirk Eibach762d3df2013-06-26 15:55:17 +02001175 pca9698_direction_output(0x22, 0, 0); /* Finder */
1176 pca9698_direction_output(0x22, 4, 0); /* Status */
1177
1178 puts("### ERROR ### Please RESET the board ###\n");
1179 bootstage_error(BOOTSTAGE_ID_NEED_RESET);
1180#ifdef CCDM_DEVELOP
1181 puts("*** ERROR ******** THIS WOULD HANG ******** ERROR ***\n");
1182 puts("** but we continue since this is a DEVELOP version **\n");
1183 puts("*** ERROR ******** THIS WOULD HANG ******** ERROR ***\n");
1184 for (j = 2; j-- > 0;) {
1185 putc('#');
1186#else
1187 for (;;) {
1188#endif
1189 f = f0;
1190 s = s0;
1191 for (i = 54; i-- > 0;) {
1192 pca9698_set_value(0x22, 0, !(f & 1));
1193 pca9698_set_value(0x22, 4, (s & 1));
1194 f >>= 1;
1195 s >>= 1;
1196 mdelay(120);
1197 }
1198 }
1199 puts("\ncontinue...\n");
1200}
1201
1202int startup_ccdm_id_module(void)
1203{
1204 int result = 0;
1205 unsigned int orig_i2c_bus;
1206
Dirk Eibache674bf12014-07-03 09:28:16 +02001207 orig_i2c_bus = i2c_get_bus_num();
1208 i2c_set_bus_num(I2C_SOC_1);
Dirk Eibach762d3df2013-06-26 15:55:17 +02001209
1210 /* goto end; */
1211
1212#ifdef CCDM_DEVELOP
1213 show_self_hash();
1214#endif
1215#ifdef CCDM_FIRST_STAGE
1216 result = first_stage_init();
1217 if (result) {
1218 puts("1st stage init failed\n");
1219 goto failure;
1220 }
1221#endif
1222#ifdef CCDM_SECOND_STAGE
1223 result = second_stage_init();
1224 if (result) {
1225 puts("2nd stage init failed\n");
1226 goto failure;
1227 }
1228#endif
1229
1230 goto end;
1231failure:
1232 result = 1;
1233end:
Dirk Eibache674bf12014-07-03 09:28:16 +02001234 i2c_set_bus_num(orig_i2c_bus);
Dirk Eibach762d3df2013-06-26 15:55:17 +02001235 if (result)
1236 ccdm_hang();
1237
1238 return result;
1239}