blob: 3da5005f4036dc6d3f9aed5f3c32c9d3f45ee944 [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 Glass1ea97892020-05-10 11:40:00 -060014#include <bootstage.h>
Simon Glassadaaa482019-11-14 12:57:43 -070015#include <command.h>
Simon Glass8ceca1d2018-11-18 14:22:27 -070016#include <dm.h>
Simon Glass6eaea252019-08-01 09:46:48 -060017#include <env.h>
Simon Glassf11478f2019-12-28 10:45:07 -070018#include <hang.h>
Dirk Eibach762d3df2013-06-26 15:55:17 +020019#include <malloc.h>
20#include <fs.h>
21#include <i2c.h>
22#include <mmc.h>
Miquel Raynal4c6759e2018-05-15 11:57:06 +020023#include <tpm-v1.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070024#include <u-boot/crc.h>
Jeroen Hofsteebfe88fe2014-06-12 22:27:12 +020025#include <u-boot/sha1.h>
Dirk Eibach762d3df2013-06-26 15:55:17 +020026#include <asm/byteorder.h>
27#include <asm/unaligned.h>
28#include <pca9698.h>
29
30#undef CCDM_FIRST_STAGE
31#undef CCDM_SECOND_STAGE
32#undef CCDM_AUTO_FIRST_STAGE
33
34#ifdef CONFIG_DEVELOP
35#define CCDM_DEVELOP
36#endif
37
38#ifdef CONFIG_TRAILBLAZER
39#define CCDM_FIRST_STAGE
40#undef CCDM_SECOND_STAGE
41#else
42#undef CCDM_FIRST_STAGE
43#define CCDM_SECOND_STAGE
44#endif
45
46#if defined(CCDM_DEVELOP) && defined(CCDM_SECOND_STAGE) && \
47 !defined(CCCM_FIRST_STAGE)
48#define CCDM_AUTO_FIRST_STAGE
49#endif
50
Dirk Eibach762d3df2013-06-26 15:55:17 +020051/* CCDM specific contants */
52enum {
53 /* NV indices */
54 NV_COMMON_DATA_INDEX = 0x40000001,
55 /* magics for key blob chains */
56 MAGIC_KEY_PROGRAM = 0x68726500,
57 MAGIC_HMAC = 0x68616300,
58 MAGIC_END_OF_CHAIN = 0x00000000,
59 /* sizes */
60 NV_COMMON_DATA_MIN_SIZE = 3 * sizeof(uint64_t) + 2 * sizeof(uint16_t),
61};
62
63/* other constants */
64enum {
65 ESDHC_BOOT_IMAGE_SIG_OFS = 0x40,
66 ESDHC_BOOT_IMAGE_SIZE_OFS = 0x48,
67 ESDHC_BOOT_IMAGE_ADDR_OFS = 0x50,
68 ESDHC_BOOT_IMAGE_TARGET_OFS = 0x58,
69 ESDHC_BOOT_IMAGE_ENTRY_OFS = 0x60,
70};
71
Dirk Eibache674bf12014-07-03 09:28:16 +020072enum {
73 I2C_SOC_0 = 0,
74 I2C_SOC_1 = 1,
75};
76
Dirk Eibach762d3df2013-06-26 15:55:17 +020077struct key_program {
78 uint32_t magic;
79 uint32_t code_crc;
80 uint32_t code_size;
81 uint8_t code[];
82};
83
84struct h_reg {
85 bool valid;
86 uint8_t digest[20];
87};
88
89
90enum access_mode {
91 HREG_NONE = 0,
92 HREG_RD = 1,
93 HREG_WR = 2,
94 HREG_RDWR = 3,
95};
96
97/* register constants */
98enum {
99 FIX_HREG_DEVICE_ID_HASH = 0,
100 FIX_HREG_SELF_HASH = 1,
101 FIX_HREG_STAGE2_HASH = 2,
102 FIX_HREG_VENDOR = 3,
103 COUNT_FIX_HREGS
104};
105
106
107/* hre opcodes */
108enum {
109 /* opcodes w/o data */
110 HRE_NOP = 0x00,
111 HRE_SYNC = HRE_NOP,
112 HRE_CHECK0 = 0x01,
113 /* opcodes w/o data, w/ sync dst */
114 /* opcodes w/ data */
115 HRE_LOAD = 0x81,
116 /* opcodes w/data, w/sync dst */
117 HRE_XOR = 0xC1,
118 HRE_AND = 0xC2,
119 HRE_OR = 0xC3,
120 HRE_EXTEND = 0xC4,
121 HRE_LOADKEY = 0xC5,
122};
123
124/* hre errors */
125enum {
126 HRE_E_OK = 0,
127 HRE_E_TPM_FAILURE,
128 HRE_E_INVALID_HREG,
129};
130
131static uint64_t device_id;
132static uint64_t device_cl;
133static uint64_t device_type;
134
135static uint32_t platform_key_handle;
136
137static void(*bl2_entry)(void);
138
139static struct h_reg pcr_hregs[24];
140static struct h_reg fix_hregs[COUNT_FIX_HREGS];
141static struct h_reg var_hregs[8];
142static uint32_t hre_tpm_err;
143static int hre_err = HRE_E_OK;
144
145#define IS_PCR_HREG(spec) ((spec) & 0x20)
146#define IS_FIX_HREG(spec) (((spec) & 0x38) == 0x08)
147#define IS_VAR_HREG(spec) (((spec) & 0x38) == 0x10)
148#define HREG_IDX(spec) ((spec) & (IS_PCR_HREG(spec) ? 0x1f : 0x7))
149
Simon Glass8ceca1d2018-11-18 14:22:27 -0700150static int get_tpm(struct udevice **devp)
151{
152 int rc;
153
154 rc = uclass_first_device_err(UCLASS_TPM, devp);
155 if (rc) {
156 printf("Could not find TPM (ret=%d)\n", rc);
157 return CMD_RET_FAILURE;
158 }
159
160 return 0;
161}
162
Dirk Eibach762d3df2013-06-26 15:55:17 +0200163static const uint8_t vendor[] = "Guntermann & Drunck";
164
Dirk Eibach762d3df2013-06-26 15:55:17 +0200165/**
166 * @brief read a bunch of data from MMC into memory.
167 *
168 * @param mmc pointer to the mmc structure to use.
169 * @param src offset where the data starts on MMC/SD device (in bytes).
170 * @param dst pointer to the location where the read data should be stored.
171 * @param size number of bytes to read from the MMC/SD device.
172 * @return number of bytes read or -1 on error.
173 */
174static int ccdm_mmc_read(struct mmc *mmc, u64 src, u8 *dst, int size)
175{
176 int result = 0;
177 u32 blk_len, ofs;
178 ulong block_no, n, cnt;
179 u8 *tmp_buf = NULL;
180
181 if (size <= 0)
182 goto end;
183
184 blk_len = mmc->read_bl_len;
185 tmp_buf = malloc(blk_len);
186 if (!tmp_buf)
187 goto failure;
188 block_no = src / blk_len;
189 ofs = src % blk_len;
190
191 if (ofs) {
Stephen Warrene73f2962015-12-07 11:38:48 -0700192 n = mmc->block_dev.block_read(&mmc->block_dev, block_no++, 1,
Dirk Eibach762d3df2013-06-26 15:55:17 +0200193 tmp_buf);
194 if (!n)
195 goto failure;
Masahiro Yamadadb204642014-11-07 03:03:31 +0900196 result = min(size, (int)(blk_len - ofs));
Dirk Eibach762d3df2013-06-26 15:55:17 +0200197 memcpy(dst, tmp_buf + ofs, result);
198 dst += result;
199 size -= result;
200 }
201 cnt = size / blk_len;
202 if (cnt) {
Stephen Warrene73f2962015-12-07 11:38:48 -0700203 n = mmc->block_dev.block_read(&mmc->block_dev, block_no, cnt,
Dirk Eibach762d3df2013-06-26 15:55:17 +0200204 dst);
205 if (n != cnt)
206 goto failure;
207 size -= cnt * blk_len;
208 result += cnt * blk_len;
209 dst += cnt * blk_len;
210 block_no += cnt;
211 }
212 if (size) {
Stephen Warrene73f2962015-12-07 11:38:48 -0700213 n = mmc->block_dev.block_read(&mmc->block_dev, block_no++, 1,
Dirk Eibach762d3df2013-06-26 15:55:17 +0200214 tmp_buf);
215 if (!n)
216 goto failure;
217 memcpy(dst, tmp_buf, size);
218 result += size;
219 }
220 goto end;
221failure:
222 result = -1;
223end:
224 if (tmp_buf)
225 free(tmp_buf);
226 return result;
227}
228
229/**
230 * @brief returns a location where the 2nd stage bootloader can be(/ is) placed.
231 *
232 * @return pointer to the location for/of the 2nd stage bootloader
233 */
234static u8 *get_2nd_stage_bl_location(ulong target_addr)
235{
236 ulong addr;
237#ifdef CCDM_SECOND_STAGE
Simon Glass22c34c22017-08-03 12:22:13 -0600238 addr = env_get_ulong("loadaddr", 16, CONFIG_LOADADDR);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200239#else
240 addr = target_addr;
241#endif
242 return (u8 *)(addr);
243}
244
245
246#ifdef CCDM_SECOND_STAGE
247/**
248 * @brief returns a location where the image can be(/ is) placed.
249 *
250 * @return pointer to the location for/of the image
251 */
252static u8 *get_image_location(void)
253{
254 ulong addr;
255 /* TODO use other area? */
Simon Glass22c34c22017-08-03 12:22:13 -0600256 addr = env_get_ulong("loadaddr", 16, CONFIG_LOADADDR);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200257 return (u8 *)(addr);
258}
259#endif
260
261/**
262 * @brief get the size of a given (TPM) NV area
263 * @param index NV index of the area to get size for
264 * @param size pointer to the size
265 * @return 0 on success, != 0 on error
266 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700267static int get_tpm_nv_size(struct udevice *tpm, uint32_t index, uint32_t *size)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200268{
269 uint32_t err;
270 uint8_t info[72];
271 uint8_t *ptr;
272 uint16_t v16;
273
Simon Glass8ceca1d2018-11-18 14:22:27 -0700274 err = tpm_get_capability(tpm, TPM_CAP_NV_INDEX, index,
275 info, sizeof(info));
Dirk Eibach762d3df2013-06-26 15:55:17 +0200276 if (err) {
277 printf("tpm_get_capability(CAP_NV_INDEX, %08x) failed: %u\n",
278 index, err);
279 return 1;
280 }
281
282 /* skip tag and nvIndex */
283 ptr = info + 6;
284 /* skip 2 pcr info fields */
285 v16 = get_unaligned_be16(ptr);
286 ptr += 2 + v16 + 1 + 20;
287 v16 = get_unaligned_be16(ptr);
288 ptr += 2 + v16 + 1 + 20;
289 /* skip permission and flags */
290 ptr += 6 + 3;
291
292 *size = get_unaligned_be32(ptr);
293 return 0;
294}
295
296/**
297 * @brief search for a key by usage auth and pub key hash.
298 * @param auth usage auth of the key to search for
299 * @param pubkey_digest (SHA1) hash of the pub key structure of the key
300 * @param[out] handle the handle of the key iff found
301 * @return 0 if key was found in TPM; != 0 if not.
302 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700303static int find_key(struct udevice *tpm, const uint8_t auth[20],
304 const uint8_t pubkey_digest[20], uint32_t *handle)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200305{
306 uint16_t key_count;
307 uint32_t key_handles[10];
308 uint8_t buf[288];
309 uint8_t *ptr;
310 uint32_t err;
311 uint8_t digest[20];
312 size_t buf_len;
313 unsigned int i;
314
315 /* fetch list of already loaded keys in the TPM */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700316 err = tpm_get_capability(tpm, TPM_CAP_HANDLE, TPM_RT_KEY, buf,
317 sizeof(buf));
Dirk Eibach762d3df2013-06-26 15:55:17 +0200318 if (err)
319 return -1;
320 key_count = get_unaligned_be16(buf);
321 ptr = buf + 2;
322 for (i = 0; i < key_count; ++i, ptr += 4)
323 key_handles[i] = get_unaligned_be32(ptr);
324
325 /* now search a(/ the) key which we can access with the given auth */
326 for (i = 0; i < key_count; ++i) {
327 buf_len = sizeof(buf);
Simon Glass8ceca1d2018-11-18 14:22:27 -0700328 err = tpm_get_pub_key_oiap(tpm, key_handles[i], auth, buf,
329 &buf_len);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200330 if (err && err != TPM_AUTHFAIL)
331 return -1;
332 if (err)
333 continue;
334 sha1_csum(buf, buf_len, digest);
335 if (!memcmp(digest, pubkey_digest, 20)) {
336 *handle = key_handles[i];
337 return 0;
338 }
339 }
340 return 1;
341}
342
343/**
344 * @brief read CCDM common data from TPM NV
345 * @return 0 if CCDM common data was found and read, !=0 if something failed.
346 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700347static int read_common_data(struct udevice *tpm)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200348{
349 uint32_t size;
350 uint32_t err;
351 uint8_t buf[256];
352 sha1_context ctx;
353
Simon Glass8ceca1d2018-11-18 14:22:27 -0700354 if (get_tpm_nv_size(tpm, NV_COMMON_DATA_INDEX, &size) ||
Dirk Eibach762d3df2013-06-26 15:55:17 +0200355 size < NV_COMMON_DATA_MIN_SIZE)
356 return 1;
Simon Glass8ceca1d2018-11-18 14:22:27 -0700357 err = tpm_nv_read_value(tpm, NV_COMMON_DATA_INDEX,
358 buf, min(sizeof(buf), size));
Dirk Eibach762d3df2013-06-26 15:55:17 +0200359 if (err) {
360 printf("tpm_nv_read_value() failed: %u\n", err);
361 return 1;
362 }
363
364 device_id = get_unaligned_be64(buf);
365 device_cl = get_unaligned_be64(buf + 8);
366 device_type = get_unaligned_be64(buf + 16);
367
368 sha1_starts(&ctx);
369 sha1_update(&ctx, buf, 24);
370 sha1_finish(&ctx, fix_hregs[FIX_HREG_DEVICE_ID_HASH].digest);
371 fix_hregs[FIX_HREG_DEVICE_ID_HASH].valid = true;
372
373 platform_key_handle = get_unaligned_be32(buf + 24);
374
375 return 0;
376}
377
378/**
379 * @brief compute hash of bootloader itself.
380 * @param[out] dst hash register where the hash should be stored
381 * @return 0 on success, != 0 on failure.
382 *
383 * @note MUST be called at a time where the boot loader is accessible at the
384 * configured location (; so take care when code is reallocated).
385 */
386static int compute_self_hash(struct h_reg *dst)
387{
388 sha1_csum((const uint8_t *)CONFIG_SYS_MONITOR_BASE,
389 CONFIG_SYS_MONITOR_LEN, dst->digest);
390 dst->valid = true;
391 return 0;
392}
393
394int ccdm_compute_self_hash(void)
395{
396 if (!fix_hregs[FIX_HREG_SELF_HASH].valid)
397 compute_self_hash(&fix_hregs[FIX_HREG_SELF_HASH]);
398 return 0;
399}
400
401/**
402 * @brief compute the hash of the 2nd stage boot loader (on SD card)
403 * @param[out] dst hash register to store the computed hash
404 * @return 0 on success, != 0 on failure
405 *
406 * Determines the size and location of the 2nd stage boot loader on SD card,
407 * loads the 2nd stage boot loader and computes the (SHA1) hash value.
408 * Within the 1st stage boot loader, the 2nd stage boot loader is loaded at
409 * the desired memory location and the variable @a bl2_entry is set.
410 *
411 * @note This sets the variable @a bl2_entry to the entry point when the
412 * 2nd stage boot loader is loaded at its configured memory location.
413 */
414static int compute_second_stage_hash(struct h_reg *dst)
415{
416 int result = 0;
417 u32 code_len, code_offset, target_addr, exec_entry;
418 struct mmc *mmc;
419 u8 *load_addr = NULL;
420 u8 buf[128];
421
422 mmc = find_mmc_device(0);
423 if (!mmc)
424 goto failure;
425 mmc_init(mmc);
426
427 if (ccdm_mmc_read(mmc, 0, buf, sizeof(buf)) < 0)
428 goto failure;
429
430 code_offset = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ADDR_OFS);
431 code_len = *(u32 *)(buf + ESDHC_BOOT_IMAGE_SIZE_OFS);
432 target_addr = *(u32 *)(buf + ESDHC_BOOT_IMAGE_TARGET_OFS);
433 exec_entry = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ENTRY_OFS);
434
435 load_addr = get_2nd_stage_bl_location(target_addr);
436 if (load_addr == (u8 *)target_addr)
437 bl2_entry = (void(*)(void))exec_entry;
438
439 if (ccdm_mmc_read(mmc, code_offset, load_addr, code_len) < 0)
440 goto failure;
441
442 sha1_csum(load_addr, code_len, dst->digest);
443 dst->valid = true;
444
445 goto end;
446failure:
447 result = 1;
448 bl2_entry = NULL;
449end:
450 return result;
451}
452
453/**
454 * @brief get pointer to hash register by specification
455 * @param spec specification of a hash register
456 * @return pointer to hash register or NULL if @a spec does not qualify a
457 * valid hash register; NULL else.
458 */
459static struct h_reg *get_hreg(uint8_t spec)
460{
461 uint8_t idx;
462
463 idx = HREG_IDX(spec);
464 if (IS_FIX_HREG(spec)) {
465 if (idx < ARRAY_SIZE(fix_hregs))
466 return fix_hregs + idx;
467 hre_err = HRE_E_INVALID_HREG;
468 } else if (IS_PCR_HREG(spec)) {
469 if (idx < ARRAY_SIZE(pcr_hregs))
470 return pcr_hregs + idx;
471 hre_err = HRE_E_INVALID_HREG;
472 } else if (IS_VAR_HREG(spec)) {
473 if (idx < ARRAY_SIZE(var_hregs))
474 return var_hregs + idx;
475 hre_err = HRE_E_INVALID_HREG;
476 }
477 return NULL;
478}
479
480/**
481 * @brief get pointer of a hash register by specification and usage.
482 * @param spec specification of a hash register
483 * @param mode access mode (read or write or read/write)
484 * @return pointer to hash register if found and valid; NULL else.
485 *
486 * This func uses @a get_reg() to determine the hash register for a given spec.
487 * If a register is found it is validated according to the desired access mode.
488 * The value of automatic registers (PCR register and fixed registers) is
489 * loaded or computed on read access.
490 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700491static struct h_reg *access_hreg(struct udevice *tpm, uint8_t spec,
492 enum access_mode mode)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200493{
494 struct h_reg *result;
495
496 result = get_hreg(spec);
497 if (!result)
498 return NULL;
499
500 if (mode & HREG_WR) {
501 if (IS_FIX_HREG(spec)) {
502 hre_err = HRE_E_INVALID_HREG;
503 return NULL;
504 }
505 }
506 if (mode & HREG_RD) {
507 if (!result->valid) {
508 if (IS_PCR_HREG(spec)) {
Simon Glass8ceca1d2018-11-18 14:22:27 -0700509 hre_tpm_err = tpm_pcr_read(tpm, HREG_IDX(spec),
Dirk Eibach762d3df2013-06-26 15:55:17 +0200510 result->digest, 20);
511 result->valid = (hre_tpm_err == TPM_SUCCESS);
512 } else if (IS_FIX_HREG(spec)) {
513 switch (HREG_IDX(spec)) {
514 case FIX_HREG_DEVICE_ID_HASH:
Simon Glass8ceca1d2018-11-18 14:22:27 -0700515 read_common_data(tpm);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200516 break;
517 case FIX_HREG_SELF_HASH:
518 ccdm_compute_self_hash();
519 break;
520 case FIX_HREG_STAGE2_HASH:
521 compute_second_stage_hash(result);
522 break;
523 case FIX_HREG_VENDOR:
524 memcpy(result->digest, vendor, 20);
525 result->valid = true;
526 break;
527 }
528 } else {
529 result->valid = true;
530 }
531 }
532 if (!result->valid) {
533 hre_err = HRE_E_INVALID_HREG;
534 return NULL;
535 }
536 }
537
538 return result;
539}
540
541static void *compute_and(void *_dst, const void *_src, size_t n)
542{
543 uint8_t *dst = _dst;
544 const uint8_t *src = _src;
545 size_t i;
546
547 for (i = n; i-- > 0; )
548 *dst++ &= *src++;
549
550 return _dst;
551}
552
553static void *compute_or(void *_dst, const void *_src, size_t n)
554{
555 uint8_t *dst = _dst;
556 const uint8_t *src = _src;
557 size_t i;
558
559 for (i = n; i-- > 0; )
560 *dst++ |= *src++;
561
562 return _dst;
563}
564
565static void *compute_xor(void *_dst, const void *_src, size_t n)
566{
567 uint8_t *dst = _dst;
568 const uint8_t *src = _src;
569 size_t i;
570
571 for (i = n; i-- > 0; )
572 *dst++ ^= *src++;
573
574 return _dst;
575}
576
577static void *compute_extend(void *_dst, const void *_src, size_t n)
578{
579 uint8_t digest[20];
580 sha1_context ctx;
581
582 sha1_starts(&ctx);
583 sha1_update(&ctx, _dst, n);
584 sha1_update(&ctx, _src, n);
585 sha1_finish(&ctx, digest);
586 memcpy(_dst, digest, min(n, sizeof(digest)));
587
588 return _dst;
589}
590
Simon Glass8ceca1d2018-11-18 14:22:27 -0700591static int hre_op_loadkey(struct udevice *tpm, struct h_reg *src_reg,
592 struct h_reg *dst_reg, const void *key,
593 size_t key_size)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200594{
595 uint32_t parent_handle;
596 uint32_t key_handle;
597
598 if (!src_reg || !dst_reg || !src_reg->valid || !dst_reg->valid)
599 return -1;
Simon Glass8ceca1d2018-11-18 14:22:27 -0700600 if (find_key(tpm, src_reg->digest, dst_reg->digest, &parent_handle))
Dirk Eibach762d3df2013-06-26 15:55:17 +0200601 return -1;
Simon Glass8ceca1d2018-11-18 14:22:27 -0700602 hre_tpm_err = tpm_load_key2_oiap(tpm, parent_handle, key, key_size,
603 src_reg->digest, &key_handle);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200604 if (hre_tpm_err) {
605 hre_err = HRE_E_TPM_FAILURE;
606 return -1;
607 }
608 /* TODO remember key handle somehow? */
609
610 return 0;
611}
612
613/**
614 * @brief executes the next opcode on the hash register engine.
615 * @param[in,out] ip pointer to the opcode (instruction pointer)
616 * @param[in,out] code_size (remaining) size of the code
617 * @return new instruction pointer on success, NULL on error.
618 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700619static const uint8_t *hre_execute_op(struct udevice *tpm, const uint8_t **ip,
620 size_t *code_size)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200621{
622 bool dst_modified = false;
623 uint32_t ins;
624 uint8_t opcode;
625 uint8_t src_spec;
626 uint8_t dst_spec;
627 uint16_t data_size;
628 struct h_reg *src_reg, *dst_reg;
629 uint8_t buf[20];
630 const uint8_t *src_buf, *data;
631 uint8_t *ptr;
632 int i;
633 void * (*bin_func)(void *, const void *, size_t);
634
635 if (*code_size < 4)
636 return NULL;
637
638 ins = get_unaligned_be32(*ip);
639 opcode = **ip;
640 data = *ip + 4;
641 src_spec = (ins >> 18) & 0x3f;
642 dst_spec = (ins >> 12) & 0x3f;
643 data_size = (ins & 0x7ff);
644
645 debug("HRE: ins=%08x (op=%02x, s=%02x, d=%02x, L=%d)\n", ins,
646 opcode, src_spec, dst_spec, data_size);
647
648 if ((opcode & 0x80) && (data_size + 4) > *code_size)
649 return NULL;
650
Simon Glass8ceca1d2018-11-18 14:22:27 -0700651 src_reg = access_hreg(tpm, src_spec, HREG_RD);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200652 if (hre_err || hre_tpm_err)
653 return NULL;
Simon Glass8ceca1d2018-11-18 14:22:27 -0700654 dst_reg = access_hreg(tpm, dst_spec,
655 (opcode & 0x40) ? HREG_RDWR : HREG_WR);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200656 if (hre_err || hre_tpm_err)
657 return NULL;
658
659 switch (opcode) {
660 case HRE_NOP:
661 goto end;
662 case HRE_CHECK0:
663 if (src_reg) {
664 for (i = 0; i < 20; ++i) {
665 if (src_reg->digest[i])
666 return NULL;
667 }
668 }
669 break;
670 case HRE_LOAD:
671 bin_func = memcpy;
672 goto do_bin_func;
673 case HRE_XOR:
674 bin_func = compute_xor;
675 goto do_bin_func;
676 case HRE_AND:
677 bin_func = compute_and;
678 goto do_bin_func;
679 case HRE_OR:
680 bin_func = compute_or;
681 goto do_bin_func;
682 case HRE_EXTEND:
683 bin_func = compute_extend;
684do_bin_func:
685 if (!dst_reg)
686 return NULL;
687 if (src_reg) {
688 src_buf = src_reg->digest;
689 } else {
690 if (!data_size) {
691 memset(buf, 0, 20);
692 src_buf = buf;
693 } else if (data_size == 1) {
694 memset(buf, *data, 20);
695 src_buf = buf;
696 } else if (data_size >= 20) {
697 src_buf = data;
698 } else {
699 src_buf = buf;
700 for (ptr = (uint8_t *)src_buf, i = 20; i > 0;
701 i -= data_size, ptr += data_size)
Masahiro Yamadadb204642014-11-07 03:03:31 +0900702 memcpy(ptr, data,
703 min_t(size_t, i, data_size));
Dirk Eibach762d3df2013-06-26 15:55:17 +0200704 }
705 }
706 bin_func(dst_reg->digest, src_buf, 20);
707 dst_reg->valid = true;
708 dst_modified = true;
709 break;
710 case HRE_LOADKEY:
Simon Glass8ceca1d2018-11-18 14:22:27 -0700711 if (hre_op_loadkey(tpm, src_reg, dst_reg, data, data_size))
Dirk Eibach762d3df2013-06-26 15:55:17 +0200712 return NULL;
713 break;
714 default:
715 return NULL;
716 }
717
718 if (dst_reg && dst_modified && IS_PCR_HREG(dst_spec)) {
Simon Glass8ceca1d2018-11-18 14:22:27 -0700719 hre_tpm_err = tpm_extend(tpm, HREG_IDX(dst_spec),
720 dst_reg->digest, dst_reg->digest);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200721 if (hre_tpm_err) {
722 hre_err = HRE_E_TPM_FAILURE;
723 return NULL;
724 }
725 }
726end:
727 *ip += 4;
728 *code_size -= 4;
729 if (opcode & 0x80) {
730 *ip += data_size;
731 *code_size -= data_size;
732 }
733
734 return *ip;
735}
736
737/**
738 * @brief runs a program on the hash register engine.
739 * @param code pointer to the (HRE) code.
740 * @param code_size size of the code (in bytes).
741 * @return 0 on success, != 0 on failure.
742 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700743static int hre_run_program(struct udevice *tpm, const uint8_t *code,
744 size_t code_size)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200745{
746 size_t code_left;
747 const uint8_t *ip = code;
748
749 code_left = code_size;
750 hre_tpm_err = 0;
751 hre_err = HRE_E_OK;
752 while (code_left > 0)
Simon Glass8ceca1d2018-11-18 14:22:27 -0700753 if (!hre_execute_op(tpm, &ip, &code_left))
Dirk Eibach762d3df2013-06-26 15:55:17 +0200754 return -1;
755
756 return hre_err;
757}
758
759static int check_hmac(struct key_program *hmac,
760 const uint8_t *data, size_t data_size)
761{
762 uint8_t key[20], computed_hmac[20];
763 uint32_t type;
764
765 type = get_unaligned_be32(hmac->code);
766 if (type != 0)
767 return 1;
768 memset(key, 0, sizeof(key));
769 compute_extend(key, pcr_hregs[1].digest, 20);
770 compute_extend(key, pcr_hregs[2].digest, 20);
771 compute_extend(key, pcr_hregs[3].digest, 20);
772 compute_extend(key, pcr_hregs[4].digest, 20);
773
774 sha1_hmac(key, sizeof(key), data, data_size, computed_hmac);
775
776 return memcmp(computed_hmac, hmac->code + 4, 20);
777}
778
779static int verify_program(struct key_program *prg)
780{
781 uint32_t crc;
782 crc = crc32(0, prg->code, prg->code_size);
783
784 if (crc != prg->code_crc) {
785 printf("HRC crc mismatch: %08x != %08x\n",
786 crc, prg->code_crc);
787 return 1;
788 }
789 return 0;
790}
791
792#if defined(CCDM_FIRST_STAGE) || (defined CCDM_AUTO_FIRST_STAGE)
793static struct key_program *load_sd_key_program(void)
794{
795 u32 code_len, code_offset;
796 struct mmc *mmc;
797 u8 buf[128];
798 struct key_program *result = NULL, *hmac = NULL;
799 struct key_program header;
800
801 mmc = find_mmc_device(0);
802 if (!mmc)
803 return NULL;
804 mmc_init(mmc);
805
806 if (ccdm_mmc_read(mmc, 0, buf, sizeof(buf)) <= 0)
807 goto failure;
808
809 code_offset = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ADDR_OFS);
810 code_len = *(u32 *)(buf + ESDHC_BOOT_IMAGE_SIZE_OFS);
811
812 code_offset += code_len;
813 /* TODO: the following needs to be the size of the 2nd stage env */
814 code_offset += CONFIG_ENV_SIZE;
815
816 if (ccdm_mmc_read(mmc, code_offset, buf, 4*3) < 0)
817 goto failure;
818
819 header.magic = get_unaligned_be32(buf);
820 header.code_crc = get_unaligned_be32(buf + 4);
821 header.code_size = get_unaligned_be32(buf + 8);
822
823 if (header.magic != MAGIC_KEY_PROGRAM)
824 goto failure;
825
826 result = malloc(sizeof(struct key_program) + header.code_size);
827 if (!result)
828 goto failure;
829 *result = header;
830
831 printf("load key program chunk from SD card (%u bytes) ",
832 header.code_size);
833 code_offset += 12;
834 if (ccdm_mmc_read(mmc, code_offset, result->code, header.code_size)
835 < 0)
836 goto failure;
837 code_offset += header.code_size;
838 puts("\n");
839
840 if (verify_program(result))
841 goto failure;
842
843 if (ccdm_mmc_read(mmc, code_offset, buf, 4*3) < 0)
844 goto failure;
845
846 header.magic = get_unaligned_be32(buf);
847 header.code_crc = get_unaligned_be32(buf + 4);
848 header.code_size = get_unaligned_be32(buf + 8);
849
850 if (header.magic == MAGIC_HMAC) {
851 puts("check integrity\n");
852 hmac = malloc(sizeof(struct key_program) + header.code_size);
853 if (!hmac)
854 goto failure;
855 *hmac = header;
856 code_offset += 12;
857 if (ccdm_mmc_read(mmc, code_offset, hmac->code,
858 hmac->code_size) < 0)
859 goto failure;
860 if (verify_program(hmac))
861 goto failure;
862 if (check_hmac(hmac, result->code, result->code_size)) {
863 puts("key program integrity could not be verified\n");
864 goto failure;
865 }
866 puts("key program verified\n");
867 }
868
869 goto end;
870failure:
871 if (result)
872 free(result);
873 result = NULL;
874end:
875 if (hmac)
876 free(hmac);
877
878 return result;
879}
880#endif
881
882#ifdef CCDM_SECOND_STAGE
883/**
884 * @brief load a key program from file system.
885 * @param ifname interface of the file system
886 * @param dev_part_str device part of the file system
887 * @param fs_type tyep of the file system
888 * @param path path of the file to load.
889 * @return the loaded structure or NULL on failure.
890 */
891static struct key_program *load_key_chunk(const char *ifname,
892 const char *dev_part_str, int fs_type,
893 const char *path)
894{
895 struct key_program *result = NULL;
896 struct key_program header;
897 uint32_t crc;
898 uint8_t buf[12];
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800899 loff_t i;
Dirk Eibach762d3df2013-06-26 15:55:17 +0200900
901 if (fs_set_blk_dev(ifname, dev_part_str, fs_type))
902 goto failure;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800903 if (fs_read(path, (ulong)buf, 0, 12, &i) < 0)
904 goto failure;
Dirk Eibach762d3df2013-06-26 15:55:17 +0200905 if (i < 12)
906 goto failure;
907 header.magic = get_unaligned_be32(buf);
908 header.code_crc = get_unaligned_be32(buf + 4);
909 header.code_size = get_unaligned_be32(buf + 8);
910
911 if (header.magic != MAGIC_HMAC && header.magic != MAGIC_KEY_PROGRAM)
912 goto failure;
913
914 result = malloc(sizeof(struct key_program) + header.code_size);
915 if (!result)
916 goto failure;
917 if (fs_set_blk_dev(ifname, dev_part_str, fs_type))
918 goto failure;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800919 if (fs_read(path, (ulong)result, 0,
920 sizeof(struct key_program) + header.code_size, &i) < 0)
921 goto failure;
Dirk Eibach762d3df2013-06-26 15:55:17 +0200922 if (i <= 0)
923 goto failure;
924 *result = header;
925
926 crc = crc32(0, result->code, result->code_size);
927
928 if (crc != result->code_crc) {
929 printf("%s: HRC crc mismatch: %08x != %08x\n",
930 path, crc, result->code_crc);
931 goto failure;
932 }
933 goto end;
934failure:
935 if (result) {
936 free(result);
937 result = NULL;
938 }
939end:
940 return result;
941}
942#endif
943
944#if defined(CCDM_FIRST_STAGE) || (defined CCDM_AUTO_FIRST_STAGE)
Tom Rini910c7932017-06-16 13:06:26 -0400945static const uint8_t prg_stage1_prepare[] = {
946 0x00, 0x20, 0x00, 0x00, /* opcode: SYNC f0 */
947 0x00, 0x24, 0x00, 0x00, /* opcode: SYNC f1 */
948 0x01, 0x80, 0x00, 0x00, /* opcode: CHECK0 PCR0 */
949 0x81, 0x22, 0x00, 0x00, /* opcode: LOAD PCR0, f0 */
950 0x01, 0x84, 0x00, 0x00, /* opcode: CHECK0 PCR1 */
951 0x81, 0x26, 0x10, 0x00, /* opcode: LOAD PCR1, f1 */
952 0x01, 0x88, 0x00, 0x00, /* opcode: CHECK0 PCR2 */
953 0x81, 0x2a, 0x20, 0x00, /* opcode: LOAD PCR2, f2 */
954 0x01, 0x8c, 0x00, 0x00, /* opcode: CHECK0 PCR3 */
955 0x81, 0x2e, 0x30, 0x00, /* opcode: LOAD PCR3, f3 */
956};
957
Simon Glass8ceca1d2018-11-18 14:22:27 -0700958static int first_stage_actions(struct udevice *tpm)
Dirk Eibach762d3df2013-06-26 15:55:17 +0200959{
960 int result = 0;
961 struct key_program *sd_prg = NULL;
962
963 puts("CCDM S1: start actions\n");
964#ifndef CCDM_SECOND_STAGE
Simon Glass8ceca1d2018-11-18 14:22:27 -0700965 if (tpm_continue_self_test(tpm))
Dirk Eibach762d3df2013-06-26 15:55:17 +0200966 goto failure;
967#else
Simon Glass8ceca1d2018-11-18 14:22:27 -0700968 tpm_continue_self_test(tpm);
Dirk Eibach762d3df2013-06-26 15:55:17 +0200969#endif
970 mdelay(37);
971
Simon Glass8ceca1d2018-11-18 14:22:27 -0700972 if (hre_run_program(tpm, prg_stage1_prepare,
973 sizeof(prg_stage1_prepare)))
Dirk Eibach762d3df2013-06-26 15:55:17 +0200974 goto failure;
975
976 sd_prg = load_sd_key_program();
977 if (sd_prg) {
Simon Glass8ceca1d2018-11-18 14:22:27 -0700978 if (hre_run_program(tpm, sd_prg->code, sd_prg->code_size))
Dirk Eibach762d3df2013-06-26 15:55:17 +0200979 goto failure;
980 puts("SD code run successfully\n");
981 } else {
982 puts("no key program found on SD\n");
983 goto failure;
984 }
985 goto end;
986failure:
987 result = 1;
988end:
989 if (sd_prg)
990 free(sd_prg);
991 printf("CCDM S1: actions done (%d)\n", result);
992 return result;
993}
994#endif
995
996#ifdef CCDM_FIRST_STAGE
997static int first_stage_init(void)
998{
Simon Glass8ceca1d2018-11-18 14:22:27 -0700999 struct udevice *tpm;
1000 int ret;
1001
Dirk Eibach762d3df2013-06-26 15:55:17 +02001002 puts("CCDM S1\n");
Simon Glass8ceca1d2018-11-18 14:22:27 -07001003 ret = get_tpm(&tpm);
1004 if (ret || tpm_init(tpm) || tpm_startup(tpm, TPM_ST_CLEAR))
Dirk Eibach762d3df2013-06-26 15:55:17 +02001005 return 1;
Simon Glass8ceca1d2018-11-18 14:22:27 -07001006 ret = first_stage_actions(tpm);
Dirk Eibach762d3df2013-06-26 15:55:17 +02001007#ifndef CCDM_SECOND_STAGE
Simon Glass8ceca1d2018-11-18 14:22:27 -07001008 if (!ret) {
Dirk Eibach762d3df2013-06-26 15:55:17 +02001009 if (bl2_entry)
1010 (*bl2_entry)();
Simon Glass8ceca1d2018-11-18 14:22:27 -07001011 ret = 1;
Dirk Eibach762d3df2013-06-26 15:55:17 +02001012 }
1013#endif
Simon Glass8ceca1d2018-11-18 14:22:27 -07001014 return ret;
Dirk Eibach762d3df2013-06-26 15:55:17 +02001015}
1016#endif
1017
1018#ifdef CCDM_SECOND_STAGE
Tom Rini09868d42017-05-08 22:14:26 -04001019static const uint8_t prg_stage2_prepare[] = {
1020 0x00, 0x80, 0x00, 0x00, /* opcode: SYNC PCR0 */
1021 0x00, 0x84, 0x00, 0x00, /* opcode: SYNC PCR1 */
1022 0x00, 0x88, 0x00, 0x00, /* opcode: SYNC PCR2 */
1023 0x00, 0x8c, 0x00, 0x00, /* opcode: SYNC PCR3 */
1024 0x00, 0x90, 0x00, 0x00, /* opcode: SYNC PCR4 */
1025};
1026
1027static const uint8_t prg_stage2_success[] = {
1028 0x81, 0x02, 0x40, 0x14, /* opcode: LOAD PCR4, #<20B data> */
1029 0x48, 0xfd, 0x95, 0x17, 0xe7, 0x54, 0x6b, 0x68, /* data */
1030 0x92, 0x31, 0x18, 0x05, 0xf8, 0x58, 0x58, 0x3c, /* data */
1031 0xe4, 0xd2, 0x81, 0xe0, /* data */
1032};
1033
1034static const uint8_t prg_stage_fail[] = {
1035 0x81, 0x01, 0x00, 0x14, /* opcode: LOAD v0, #<20B data> */
1036 0xc0, 0x32, 0xad, 0xc1, 0xff, 0x62, 0x9c, 0x9b, /* data */
1037 0x66, 0xf2, 0x27, 0x49, 0xad, 0x66, 0x7e, 0x6b, /* data */
1038 0xea, 0xdf, 0x14, 0x4b, /* data */
1039 0x81, 0x42, 0x30, 0x00, /* opcode: LOAD PCR3, v0 */
1040 0x81, 0x42, 0x40, 0x00, /* opcode: LOAD PCR4, v0 */
1041};
1042
Dirk Eibach762d3df2013-06-26 15:55:17 +02001043static int second_stage_init(void)
1044{
1045 static const char mac_suffix[] = ".mac";
1046 bool did_first_stage_run = true;
1047 int result = 0;
1048 char *cptr, *mmcdev = NULL;
1049 struct key_program *hmac_blob = NULL;
1050 const char *image_path = "/ccdm.itb";
1051 char *mac_path = NULL;
1052 ulong image_addr;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001053 loff_t image_size;
Simon Glass8ceca1d2018-11-18 14:22:27 -07001054 struct udevice *tpm;
Dirk Eibach762d3df2013-06-26 15:55:17 +02001055 uint32_t err;
Simon Glass8ceca1d2018-11-18 14:22:27 -07001056 int ret;
Dirk Eibach762d3df2013-06-26 15:55:17 +02001057
1058 printf("CCDM S2\n");
Simon Glass8ceca1d2018-11-18 14:22:27 -07001059 ret = get_tpm(&tpm);
1060 if (ret || tpm_init(tpm))
Dirk Eibach762d3df2013-06-26 15:55:17 +02001061 return 1;
Simon Glass8ceca1d2018-11-18 14:22:27 -07001062 err = tpm_startup(tpm, TPM_ST_CLEAR);
Dirk Eibach762d3df2013-06-26 15:55:17 +02001063 if (err != TPM_INVALID_POSTINIT)
1064 did_first_stage_run = false;
1065
1066#ifdef CCDM_AUTO_FIRST_STAGE
Simon Glass8ceca1d2018-11-18 14:22:27 -07001067 if (!did_first_stage_run && first_stage_actions(tpm))
Dirk Eibach762d3df2013-06-26 15:55:17 +02001068 goto failure;
1069#else
1070 if (!did_first_stage_run)
1071 goto failure;
1072#endif
1073
Simon Glass8ceca1d2018-11-18 14:22:27 -07001074 if (hre_run_program(tpm, prg_stage2_prepare,
1075 sizeof(prg_stage2_prepare)))
Dirk Eibach762d3df2013-06-26 15:55:17 +02001076 goto failure;
1077
1078 /* run "prepboot" from env to get "mmcdev" set */
Simon Glass64b723f2017-08-03 12:22:12 -06001079 cptr = env_get("prepboot");
Dirk Eibach762d3df2013-06-26 15:55:17 +02001080 if (cptr && !run_command(cptr, 0))
Simon Glass64b723f2017-08-03 12:22:12 -06001081 mmcdev = env_get("mmcdev");
Dirk Eibach762d3df2013-06-26 15:55:17 +02001082 if (!mmcdev)
1083 goto failure;
1084
Simon Glass64b723f2017-08-03 12:22:12 -06001085 cptr = env_get("ramdiskimage");
Dirk Eibach762d3df2013-06-26 15:55:17 +02001086 if (cptr)
1087 image_path = cptr;
1088
1089 mac_path = malloc(strlen(image_path) + strlen(mac_suffix) + 1);
1090 if (mac_path == NULL)
1091 goto failure;
1092 strcpy(mac_path, image_path);
1093 strcat(mac_path, mac_suffix);
1094
1095 /* read image from mmcdev (ccdm.itb) */
1096 image_addr = (ulong)get_image_location();
1097 if (fs_set_blk_dev("mmc", mmcdev, FS_TYPE_EXT))
1098 goto failure;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001099 if (fs_read(image_path, image_addr, 0, 0, &image_size) < 0)
1100 goto failure;
Dirk Eibach762d3df2013-06-26 15:55:17 +02001101 if (image_size <= 0)
1102 goto failure;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001103 printf("CCDM image found on %s, %lld bytes\n", mmcdev, image_size);
Dirk Eibach762d3df2013-06-26 15:55:17 +02001104
1105 hmac_blob = load_key_chunk("mmc", mmcdev, FS_TYPE_EXT, mac_path);
1106 if (!hmac_blob) {
1107 puts("failed to load mac file\n");
1108 goto failure;
1109 }
1110 if (verify_program(hmac_blob)) {
1111 puts("corrupted mac file\n");
1112 goto failure;
1113 }
1114 if (check_hmac(hmac_blob, (u8 *)image_addr, image_size)) {
1115 puts("image integrity could not be verified\n");
1116 goto failure;
1117 }
1118 puts("CCDM image OK\n");
1119
Simon Glass8ceca1d2018-11-18 14:22:27 -07001120 hre_run_program(tpm, prg_stage2_success, sizeof(prg_stage2_success));
Dirk Eibach762d3df2013-06-26 15:55:17 +02001121
1122 goto end;
1123failure:
1124 result = 1;
Simon Glass8ceca1d2018-11-18 14:22:27 -07001125 hre_run_program(tpm, prg_stage_fail, sizeof(prg_stage_fail));
Dirk Eibach762d3df2013-06-26 15:55:17 +02001126end:
1127 if (hmac_blob)
1128 free(hmac_blob);
1129 if (mac_path)
1130 free(mac_path);
1131
1132 return result;
1133}
1134#endif
1135
1136int show_self_hash(void)
1137{
1138 struct h_reg *hash_ptr;
1139#ifdef CCDM_SECOND_STAGE
1140 struct h_reg hash;
1141
1142 hash_ptr = &hash;
1143 if (compute_self_hash(hash_ptr))
1144 return 1;
1145#else
1146 hash_ptr = &fix_hregs[FIX_HREG_SELF_HASH];
1147#endif
1148 puts("self hash: ");
1149 if (hash_ptr && hash_ptr->valid)
1150 print_buffer(0, hash_ptr->digest, 1, 20, 20);
1151 else
1152 puts("INVALID\n");
1153
1154 return 0;
1155}
1156
1157/**
1158 * @brief let the system hang.
1159 *
1160 * Called on error.
1161 * Will stop the boot process; display a message and signal the error condition
1162 * by blinking the "status" and the "finder" LED of the controller board.
1163 *
1164 * @note the develop version runs the blink cycle 2 times and then returns.
1165 * The release version never returns.
1166 */
1167static void ccdm_hang(void)
1168{
1169 static const u64 f0 = 0x0ba3bb8ba2e880; /* blink code "finder" LED */
1170 static const u64 s0 = 0x00f0f0f0f0f0f0; /* blink code "status" LED */
1171 u64 f, s;
1172 int i;
1173#ifdef CCDM_DEVELOP
1174 int j;
1175#endif
1176
Dirk Eibache674bf12014-07-03 09:28:16 +02001177 I2C_SET_BUS(I2C_SOC_0);
Dirk Eibach762d3df2013-06-26 15:55:17 +02001178 pca9698_direction_output(0x22, 0, 0); /* Finder */
1179 pca9698_direction_output(0x22, 4, 0); /* Status */
1180
1181 puts("### ERROR ### Please RESET the board ###\n");
1182 bootstage_error(BOOTSTAGE_ID_NEED_RESET);
1183#ifdef CCDM_DEVELOP
1184 puts("*** ERROR ******** THIS WOULD HANG ******** ERROR ***\n");
1185 puts("** but we continue since this is a DEVELOP version **\n");
1186 puts("*** ERROR ******** THIS WOULD HANG ******** ERROR ***\n");
1187 for (j = 2; j-- > 0;) {
1188 putc('#');
1189#else
1190 for (;;) {
1191#endif
1192 f = f0;
1193 s = s0;
1194 for (i = 54; i-- > 0;) {
1195 pca9698_set_value(0x22, 0, !(f & 1));
1196 pca9698_set_value(0x22, 4, (s & 1));
1197 f >>= 1;
1198 s >>= 1;
1199 mdelay(120);
1200 }
1201 }
1202 puts("\ncontinue...\n");
1203}
1204
1205int startup_ccdm_id_module(void)
1206{
1207 int result = 0;
1208 unsigned int orig_i2c_bus;
1209
Dirk Eibache674bf12014-07-03 09:28:16 +02001210 orig_i2c_bus = i2c_get_bus_num();
1211 i2c_set_bus_num(I2C_SOC_1);
Dirk Eibach762d3df2013-06-26 15:55:17 +02001212
1213 /* goto end; */
1214
1215#ifdef CCDM_DEVELOP
1216 show_self_hash();
1217#endif
1218#ifdef CCDM_FIRST_STAGE
1219 result = first_stage_init();
1220 if (result) {
1221 puts("1st stage init failed\n");
1222 goto failure;
1223 }
1224#endif
1225#ifdef CCDM_SECOND_STAGE
1226 result = second_stage_init();
1227 if (result) {
1228 puts("2nd stage init failed\n");
1229 goto failure;
1230 }
1231#endif
1232
1233 goto end;
1234failure:
1235 result = 1;
1236end:
Dirk Eibache674bf12014-07-03 09:28:16 +02001237 i2c_set_bus_num(orig_i2c_bus);
Dirk Eibach762d3df2013-06-26 15:55:17 +02001238 if (result)
1239 ccdm_hang();
1240
1241 return result;
1242}