version_254干啥的
xref: /MT6769_15.0.0_release/vnd/vendor/mediatek/proprietary/bootable/bootloader/lk/platform/common/storage/mmc/emmc_upgrade.c
HomeAnnotateLine# Scopes# Navigate#Raw Download
current directory
1 /***********************************************************
2 ** File: - emmc_upgrade.c
3 ** Description: Source file for emmc upgrade.
4 ** To do FFU by vendor spec.
5 ** Version: 1.0
6 ** Date : 2017/08/14
7 ** Author: Hao.Yu@PSW.BSP.Storage.Emmc
8 **
9 ** ------------------------------- Revision History:
10 -------------------------------
11 ** <author> <data> <version > <desc>
12 ** Hao.Yu 2017/08/14 1.0 build this module
13 ****************************************************************/
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <arch/defines.h>
17 #include <malloc.h>
18 #include <debug.h>
19 #include <target.h>
20 #include <mmc_core.h>
21 #include <msdc.h>
22 #include <partition.h>
23 #include "emmc_upgrade.h"
24 //#include <oppo_project.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <kernel/thread.h>
28
29
30 #define FIRMWARE_VERSION_INDEX 254
31 #define FIRMWARE_VERSION_BYTE 8
32 #define FIRMWARE_TMP_VERSION_INDEX 306
33
34 #define FIRMWARE_CONFIG_INDEX 30
35 #define FIRMWARE_DATA_INDEX 0x0000FFFF
36 #define FIRMWARE_UPGRADE_VERIFY_INDEX 26
37 #define SDHCI_MMC_BLK_SZ 512
38 #define BRIDGE_VER (0xFF)
39 unsigned int EMMC_nMagicNum1 = 0x9FB73A5A;
40 unsigned int EMMC_nMagicNum2 = 0x7B6CE6FA;
41 struct fwhead {
42 unsigned int Magic1;
43 unsigned int Magic2;
44 unsigned long long manfid;
45 char prod_name[8];
46 unsigned long long Version_prv;
47 unsigned long long Version_254;
48 unsigned long long Version_306;
49 unsigned int len;
50 unsigned int crc_fw;
51 unsigned int crc_head; //this value should be zero when calculate crc
52 unsigned int flag;// this value also should be zero when calculate crc
53 };
54
55 static int nfirmware_length = 0; //real fw length is block size aligned
56 #define FIRMWARE_LENGTH_MAX 524288
57 //static uint8_t firmware_buffer[FIRMWARE_LENGTH_MAX] = {0};
58
59 #define debug_info(format, args...) dprintf(CRITICAL, "%s:"format,__func__,##args)
60
61 static int check_firmware_version(struct mmc_host *host, struct mmc_card *card,
62 unsigned long long* pVersion_prv,
63 unsigned long long* pVersion_254,
64 unsigned long long* pVersion_306)
65 {
66 int i = 0, val = 0;
67 unsigned long long ver = 0;
68
69 val = mmc_read_ext_csd(host, card);
70 if (val) {
71 dprintf(CRITICAL, "Failed to get extCSD\n");
72 return -1;
73 }
74
75 *pVersion_prv = card->cid.prv;
76 debug_info("Emmc pVersion_prv(cid.prv) = 0x%02x\n",card->cid.prv);
77 *pVersion_254 = 0;
78 for(i = 0; i < FIRMWARE_VERSION_BYTE; i++)
79 {
80 ver = (card->raw_ext_csd[FIRMWARE_VERSION_INDEX + i] & 0xFF);
81 *pVersion_254 |= ver << (8 * i);
82 debug_info("pVersion_254=0x%llx ver=0x%llx index[%d]=0x%02x &ff=0x%02x\n",
83 *pVersion_254,ver,FIRMWARE_VERSION_INDEX+i,card->raw_ext_csd[FIRMWARE_VERSION_INDEX + i],
84 (card->raw_ext_csd[FIRMWARE_VERSION_INDEX + i] & 0xFF));
85 }
86 *pVersion_306 = card->raw_ext_csd[FIRMWARE_TMP_VERSION_INDEX];
87
88 return 0;
89 }
90
91 static unsigned int CRC32_table[256] = {0};
92
93 static void InitCRC32table(void)
94 {
95 int i = 0,j = 0;
96 unsigned int crc = 0x00;
97 if(CRC32_table[255] != 0)
98 {
99 return;
100 }
101 for (i = 0; i < 256; i++)
102 {
103 crc = i;
104 for (j = 0; j < 8; j++)
105 {
106 if (crc & 1)
107 crc = (crc >> 1) ^ 0xFCD8B5A3;
108 else
109 crc >>= 1;
110 }
111 CRC32_table[i] = crc;
112 }
113 }
114
115 static unsigned int GetCRC32(unsigned char* buf, unsigned int len)
116 {
117 unsigned int i = 0,t = 0;
118 unsigned int crc32_data = 0xFFFFFFFF;
119 //init crc table first
120 InitCRC32table();
121 for (i = 0; i < len; i++)
122 {
123 t = (crc32_data ^ buf[i]) & 0xFF;
124 crc32_data = ((crc32_data >> 8) & 0xFFFFFFFF) ^ CRC32_table[t];
125 }
126 return ~crc32_data;
127 }
128
129 unsigned long long reverse_bytes_long(unsigned long long num)
130 {
131 unsigned long long result = 0;
132 for (int i = 0; i < 8; i++)
133 {
134 result |= ((num >> (i * 8)) & 0xff) << ((7 - i) * 8);
135 }
136 return result;
137 }
138
139 uint32_t need_upgrade_emmc_fimware(unsigned long long firmware_offset, unsigned char *fw_buff)
140 {
141 uint64_t local_version = 0xFFFFFFFF00000000;
142 int img_index = -1;
143 unsigned long long img_ptn = 0, version_prv = 0, version_254 = 0, version_306 = 0;
144 //unsigned long long firmware_offset = 0;
145 unsigned char buf[SDHCI_MMC_BLK_SZ] = {0};
146 unsigned int local_crc = 0;
147 unsigned int crc_tmp = 0;
148 struct fwhead *pfwhead = NULL;
149 #ifdef MTK_EMMC_SUPPORT
150 struct mmc_host *host;
151 #endif
152 struct mmc_card *card;
153
154 card=mmc_get_card(0);
155 #ifdef MTK_EMMC_SUPPORT
156 host=mmc_get_host(0);
157 #endif
158
159 #ifdef CLOSE_EMMC_FW_UPGRADE
160 return 0;
161 #endif
162 /*
163 **We need read frimware from reserve3 partition
164 **to avoid cost too much aboot memory space
165 */
166
167 #ifdef OPPO_RESERVE_USE_LEGACY
168 img_index = partition_get_index("reserve4");
169 #else
170 img_index = partition_get_index("oplusreserve1");
171 #endif
172 img_ptn = partition_get_offset(img_index);
173 debug_info("img_index=%d img_ptn=%lld\n",img_index,img_ptn);
174
175 if (mmc_block_read(MMC_HOST_ID, (img_ptn + firmware_offset)/SDHCI_MMC_BLK_SZ, 1, (unsigned int *) buf)) {
176 debug_info("ERROR: Cannot read emmc firmware head from reserve3 partition\n");
177 return 0;
178 }
179
180 pfwhead = (struct fwhead *)buf;
181
182 if(pfwhead->Magic1 != EMMC_nMagicNum1 || pfwhead->Magic2 != EMMC_nMagicNum2){
183 debug_info("ERROR: emmc fw head magic error!\n");
184 debug_info("ERROR: magic1 =0x%x magic2 =0x%x!\n",pfwhead->Magic1,pfwhead->Magic2);
185 return 0;
186 }
187
188 if(pfwhead->manfid != card->cid.manfid || strncmp((const char*)card->cid.prod_name, (const char*)pfwhead->prod_name, strlen((const char*)card->cid.prod_name))){
189 debug_info("ERROR: card->cid.manfid=0x%x card->cid.prod_name=%s\n", card->cid.manfid, card->cid.prod_name);
190 debug_info("ERROR: pfwhead->manfid=0x%x, pfwhead->prod_name=%s\n",pfwhead->manfid, (const char*)pfwhead->prod_name);
191 return 0;
192 }
193
194 //check head crc
195 crc_tmp = pfwhead->crc_head;
196 pfwhead->crc_head = 0;
197 local_crc = GetCRC32((unsigned char*)pfwhead,sizeof(struct fwhead));
198
199 if(local_crc != crc_tmp){
200 debug_info("ERROR: crc head = %x local = %x !\n",crc_tmp,local_crc);
201 return 0;
202 }
203 nfirmware_length = pfwhead->len;
204 if(nfirmware_length > FIRMWARE_LENGTH_MAX){
205 debug_info("ERROR: nfirmware_length is too long !\n");
206 return 0;
207 }
208 if(pfwhead->flag>= 2){
209 debug_info("ERROR: emmc fw update more then 2 times!\n");
210 return 0;
211 }
212 //caculate new crc and update back to emmc
213 pfwhead->flag++;
214 crc_tmp = GetCRC32((unsigned char*)pfwhead,sizeof(struct fwhead));
215 pfwhead->crc_head = crc_tmp;
216
217 if (mmc_block_write(MMC_HOST_ID, (img_ptn + firmware_offset)/SDHCI_MMC_BLK_SZ, 1, (void*)pfwhead))
218 {
219 debug_info("ERROR:write flag fail, return error\n");
220 return 0;
221 }
222
223 if (check_firmware_version(host, card, &version_prv, &version_254, &version_306))
224 {
225 debug_info("ERROR:check_firmware_version fail\n");
226 return 0;
227 }
228
229 debug_info("fw_version_prv = 0x%llx, fw_version_254 = 0x%llx, fw_version_306 = 0x%llx\n"
230 "local_version_prv = 0x%llx, local_version_254 = 0x%llx, local_version_306 = 0x%llx\n",
231 pfwhead->Version_prv, pfwhead->Version_254, pfwhead->Version_306,
232 version_prv, version_254, version_306);
233 if ((reverse_bytes_long(version_254) < reverse_bytes_long(pfwhead->Version_prv)) ||
234 ((version_prv == pfwhead->Version_prv) && ((reverse_bytes_long(version_254) < reverse_bytes_long(pfwhead->Version_254))
235 || (version_306 < pfwhead->Version_306))) || (reverse_bytes_long(version_254) == BRIDGE_VER))
236 {
237 //check crc
238 if (mmc_block_read(MMC_HOST_ID,
239 (img_ptn + firmware_offset)/SDHCI_MMC_BLK_SZ + 1,
240 nfirmware_length/SDHCI_MMC_BLK_SZ,
241 (unsigned long*)fw_buff))
242 {
243 debug_info("ERROR: Cannot read emmc firmware from logo partition\n");
244 return 0;
245 }
246 local_crc = GetCRC32(fw_buff,nfirmware_length);
247 if(local_crc != pfwhead->crc_fw){
248 debug_info("ERROR: crc error\n");
249 #if 1
250 //in this case, invalid the fw header, which will trigger the fw move again
251 memset(pfwhead, 0, sizeof(struct fwhead));
252 mmc_block_write(MMC_HOST_ID, (img_ptn + firmware_offset)/SDHCI_MMC_BLK_SZ, 1, (void*)pfwhead);
253 #endif
254 return 0;
255 }
256 return 1;
257 }
258 return 0;
259 }
260
261 static uint32_t mmc_set_ffu_mode(struct mmc_host *host, struct mmc_card *card, uint8_t byte)
262 {
263 uint32_t ret;
264 /* Set MODE_CONFIG value of EXT_CSD[30] */
265 ret = mmc_switch(host, card, EXT_CSD_CMD_SET_NORMAL, 30/* MODE_CONFIG */, byte,MMC_SWITCH_MODE_WRITE_BYTE);
266 if (ret)
267 {
268 dprintf(CRITICAL, "Failed to write MODE_CONFIG of EXT_CSD[30]\n");
269 }
270 return ret;
271 }
272
273
274 uint32_t emmc_firmware_upgrade_flow(unsigned long vendor_argu, unsigned char *fw_buff)
275 {
276 uint32_t val = 0;
277 #ifdef MTK_EMMC_SUPPORT
278 struct mmc_host *host;
279 #endif
280 struct mmc_card *card;
281
282 card=mmc_get_card(0);
283 #ifdef MTK_EMMC_SUPPORT
284 host=mmc_get_host(0);
285 #endif
286
287 if(nfirmware_length == 0)
288 return RETURN_UPGRADE_FAIL;
289
290 /* Enter FFU mode */
291 val = mmc_set_ffu_mode(host, card, 1);
292 if (val) {
293 debug_info("Failed Writing 0x01 to MODE_CONFIG\n");
294 return val;
295 }
296
297 /* Write FW data */
298 val = mmc_block_write(MMC_HOST_ID,
299 vendor_argu /* FFU_ARG */,
300 (nfirmware_length / SDHCI_MMC_BLK_SZ),
301 (void *)fw_buff);
302 if (val) {
303 debug_info("Failed Writing eMMC Firmware\n");
304 return val;
305 }
306
307 /* Exit FFU mode */
308 val = mmc_set_ffu_mode(host, card, 0);
309 if (val) {
310 debug_info("Failed Writing 0x00 to MODE_CONFIG\n");
311 return val;
312 }
313 debug_info("FFU is done. Reboot board.\n");
314 return val;
315
316 }
317
318
served by {OpenGrok
Last Index Update: Thu Aug 07 23:03:58 CST 2025version_254干啥的,Version_prv干啥的,如果if ((reverse_bytes_long(version_254) < reverse_bytes_long(pfwhead->Version_prv)) || 换成((reverse_bytes_long(Version_prv) < reverse_bytes_long(pfwhead->Version_prv)) || 会怎么样
最新发布