Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Albert ARIBAUD \(3ADEV\) | 2e9f494 | 2016-09-26 09:08:06 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Image manipulator for Vybrid SoCs |
| 4 | * |
| 5 | * Derived from vybridimage.c |
| 6 | * |
| 7 | * (C) Copyright 2016 DENX Software Engineering GmbH |
| 8 | * Written-by: Albert ARIBAUD <albert.aribaud@3adev.fr> |
Albert ARIBAUD \(3ADEV\) | 2e9f494 | 2016-09-26 09:08:06 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include "imagetool.h" |
| 12 | #include <compiler.h> |
| 13 | #include <image.h> |
| 14 | |
| 15 | /* |
| 16 | * NAND page 0 boot header |
| 17 | */ |
| 18 | |
| 19 | struct nand_page_0_boot_header { |
| 20 | union { |
| 21 | uint32_t fcb[128]; |
| 22 | uint8_t fcb_bytes[512]; |
| 23 | }; /* 0x00000000 - 0x000001ff */ |
| 24 | uint8_t sw_ecc[512]; /* 0x00000200 - 0x000003ff */ |
| 25 | uint32_t padding[65280]; /* 0x00000400 - 0x0003ffff */ |
| 26 | uint8_t ivt_prefix[1024]; /* 0x00040000 - 0x000403ff */ |
| 27 | }; |
| 28 | |
| 29 | /* signature byte for a readable block */ |
| 30 | |
| 31 | static struct nand_page_0_boot_header vybridimage_header; |
| 32 | |
| 33 | static int vybridimage_check_image_types(uint8_t type) |
| 34 | { |
| 35 | if (type == IH_TYPE_VYBRIDIMAGE) |
| 36 | return EXIT_SUCCESS; |
| 37 | return EXIT_FAILURE; |
| 38 | } |
| 39 | |
| 40 | static uint8_t vybridimage_sw_ecc(uint8_t byte) |
| 41 | { |
| 42 | uint8_t bit0 = (byte & (1 << 0)) ? 1 : 0; |
| 43 | uint8_t bit1 = (byte & (1 << 1)) ? 1 : 0; |
| 44 | uint8_t bit2 = (byte & (1 << 2)) ? 1 : 0; |
| 45 | uint8_t bit3 = (byte & (1 << 3)) ? 1 : 0; |
| 46 | uint8_t bit4 = (byte & (1 << 4)) ? 1 : 0; |
| 47 | uint8_t bit5 = (byte & (1 << 5)) ? 1 : 0; |
| 48 | uint8_t bit6 = (byte & (1 << 6)) ? 1 : 0; |
| 49 | uint8_t bit7 = (byte & (1 << 7)) ? 1 : 0; |
| 50 | uint8_t res = 0; |
| 51 | |
| 52 | res |= ((bit6 ^ bit5 ^ bit3 ^ bit2) << 0); |
| 53 | res |= ((bit7 ^ bit5 ^ bit4 ^ bit2 ^ bit1) << 1); |
| 54 | res |= ((bit7 ^ bit6 ^ bit5 ^ bit1 ^ bit0) << 2); |
| 55 | res |= ((bit7 ^ bit4 ^ bit3 ^ bit0) << 3); |
| 56 | res |= ((bit6 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0) << 4); |
| 57 | |
| 58 | return res; |
| 59 | } |
| 60 | |
| 61 | static int vybridimage_verify_header(unsigned char *ptr, int image_size, |
| 62 | struct image_tool_params *params) |
| 63 | { |
| 64 | struct nand_page_0_boot_header *hdr = |
| 65 | (struct nand_page_0_boot_header *)ptr; |
| 66 | int idx; |
| 67 | |
| 68 | if (hdr->fcb[1] != 0x46434220) |
| 69 | return -1; |
| 70 | if (hdr->fcb[2] != 1) |
| 71 | return -1; |
| 72 | if (hdr->fcb[7] != 64) |
| 73 | return -1; |
| 74 | if (hdr->fcb[14] != 6) |
| 75 | return -1; |
| 76 | if (hdr->fcb[30] != 0x0001ff00) |
| 77 | return -1; |
| 78 | if (hdr->fcb[43] != 1) |
| 79 | return -1; |
| 80 | if (hdr->fcb[54] != 0) |
| 81 | return -1; |
| 82 | if (hdr->fcb[55] != 8) |
| 83 | return -1; |
| 84 | |
| 85 | /* check software ECC */ |
| 86 | for (idx = 0; idx < sizeof(hdr->fcb_bytes); idx++) { |
| 87 | uint8_t sw_ecc = vybridimage_sw_ecc(hdr->fcb_bytes[idx]); |
| 88 | if (sw_ecc != hdr->sw_ecc[idx]) |
| 89 | return -1; |
| 90 | } |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static void vybridimage_set_header(void *ptr, struct stat *sbuf, int ifd, |
| 96 | struct image_tool_params *params) |
| 97 | { |
| 98 | struct nand_page_0_boot_header *hdr = |
| 99 | (struct nand_page_0_boot_header *)ptr; |
| 100 | int idx; |
| 101 | |
| 102 | /* fill header with 0x00 for first 56 entries then 0xff */ |
| 103 | memset(&hdr->fcb[0], 0x0, 56*sizeof(uint32_t)); |
| 104 | memset(&hdr->fcb[56], 0xff, 72*sizeof(uint32_t)); |
| 105 | /* fill SW ecc and padding with 0xff */ |
| 106 | memset(&hdr->sw_ecc[0], 0xff, sizeof(hdr->sw_ecc)); |
| 107 | memset(&hdr->padding[0], 0xff, sizeof(hdr->padding)); |
| 108 | /* fill IVT prefix with 0x00 */ |
| 109 | memset(&hdr->ivt_prefix[0], 0x00, sizeof(hdr->ivt_prefix)); |
| 110 | |
| 111 | /* populate fcb */ |
| 112 | hdr->fcb[1] = 0x46434220; /* signature */ |
| 113 | hdr->fcb[2] = 0x00000001; /* version */ |
| 114 | hdr->fcb[5] = 2048; /* page size */ |
| 115 | hdr->fcb[6] = (2048+64); /* page + OOB size */ |
| 116 | hdr->fcb[7] = 64; /* pages per block */ |
| 117 | hdr->fcb[14] = 6; /* ECC mode 6 */ |
| 118 | hdr->fcb[26] = 128; /* fw address (0x40000) in 2K pages */ |
| 119 | hdr->fcb[27] = 128; /* fw address (0x40000) in 2K pages */ |
| 120 | hdr->fcb[30] = 0x0001ff00; /* DBBT search area start address */ |
| 121 | hdr->fcb[33] = 2048; /* BB marker physical offset */ |
| 122 | hdr->fcb[43] = 1; /* DISBBM */ |
| 123 | hdr->fcb[54] = 0; /* DISBB_Search */ |
| 124 | hdr->fcb[55] = 8; /* Bad block search limit */ |
| 125 | |
| 126 | /* compute software ECC */ |
| 127 | for (idx = 0; idx < sizeof(hdr->fcb_bytes); idx++) |
| 128 | hdr->sw_ecc[idx] = vybridimage_sw_ecc(hdr->fcb_bytes[idx]); |
| 129 | } |
| 130 | |
| 131 | static void vybridimage_print_hdr_field(struct nand_page_0_boot_header *hdr, |
| 132 | int idx) |
| 133 | { |
| 134 | printf("header.fcb[%d] = %08x\n", idx, hdr->fcb[idx]); |
| 135 | } |
| 136 | |
| 137 | static void vybridimage_print_header(const void *ptr) |
| 138 | { |
| 139 | struct nand_page_0_boot_header *hdr = |
| 140 | (struct nand_page_0_boot_header *)ptr; |
| 141 | int idx; |
| 142 | |
| 143 | for (idx = 0; idx < 56; idx++) |
| 144 | vybridimage_print_hdr_field(hdr, idx); |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * vybridimage parameters |
| 149 | */ |
| 150 | U_BOOT_IMAGE_TYPE( |
| 151 | vybridimage, |
| 152 | "Vybrid Boot Image", |
| 153 | sizeof(vybridimage_header), |
| 154 | (void *)&vybridimage_header, |
| 155 | NULL, |
| 156 | vybridimage_verify_header, |
| 157 | vybridimage_print_header, |
| 158 | vybridimage_set_header, |
| 159 | NULL, |
| 160 | vybridimage_check_image_types, |
| 161 | NULL, |
| 162 | NULL |
| 163 | ); |