blob: 1ae6ffe3c115dfdc342eb0e2ca106fdbc361c546 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2018 MediaTek Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24#include <malloc.h>
25#include <platform/mtk_wdt.h>
26#include <stdint.h>
27#include <string.h>
28
29#include "aee.h"
30#include "kdump.h"
31#include "kdump_sdhc.h"
32
33#define PAGE_SIZE 4096
34
35#define DEVICE_SECTOR_BYTES 512
36
37FAT_Para m_bFATInfo;
38uint32_t m_bLastFATPage;
39struct aee_timer total_time;
40static struct mrdump_dev *dumpdev;
41
42LfnEntry g_LfnEntry = {
43 0x41, // sequence number
44 {'C','\0','E','\0','D','\0','u','\0','m','\0'}, // name characters (five UTF-16 characters)
45 0x0F, // attributes (always 0x0F)
46 0x00, // reserved (alwyas 0x00)
47 0xDF, // checksum of DOS file name
48 {'p','\0','.','\0','k','\0','d','\0','m','\0','p','\0'}, // name characters (six UTF-16 characters)
49 0x0000, // word of first cluster (always 0x0000)
50 {'\0','\0',0xFF,0xFF}
51};
52
53DirEntry g_DirEntry = {
54 {'C','E','D','U','M','P','~','1','K','D','M'}, // name
55 0x20, // attr
56 0x00, // NTflags
57 0x00, // createdTimeMsec
58 0x63E8, // createdTime
59 0x2E21, // createdDate
60 0x2E21, // lastAccessDate
61 0x0000, // clusFirstHigh
62 0x6490, // time
63 0x2E21, // date
64 0x0000, // clusFirst
65 0x00000000 // size
66};
67
68unsigned int OALGetTickCount(void)
69{
70 return 0 ;
71}
72
73static unsigned char ToLower(unsigned char c)
74{
75 if ((c >= 'A') && (c <= 'Z'))
76 return c+ 'a' - 'A';
77 else
78 return c;
79}
80
81static bool Compare_sd(const uint8_t *a, const uint8_t *b, int length)
82{
83 while (length--) {
84 if (ToLower(*a++) != ToLower(*b++))
85 return false;
86 }
87 return true;
88}
89
90static uint32_t BytesToNum_sd(uint8_t *b, int bytes)
91{
92 uint32_t result = 0;
93 int i;
94
95 for (i = 0; i < bytes; i++) {
96 result |= b[i] << (i << 3);
97 }
98 return result;
99}
100
101static bool fatfs_dev_read(struct mrdump_dev *dev, uint64_t sector_addr, uint8_t *pdBuf, int32_t blockLen)
102{
103 return dev->read(dev, sector_addr * DEVICE_SECTOR_BYTES, pdBuf, blockLen * DEVICE_SECTOR_BYTES);
104}
105
106static bool fatfs_dev_write(struct mrdump_dev *dev, uint64_t sector_addr, uint8_t *pdBuf, int32_t blockLen)
107{
108 return dev->write(dev, sector_addr * DEVICE_SECTOR_BYTES, pdBuf, blockLen * DEVICE_SECTOR_BYTES);
109}
110
111static void fatfs_commit_fat_entry(FileHandler *pFileHandler, int fat_offset, uint32_t value)
112{
113 if (m_bFATInfo.FileSysType == FAT_32) {
114 pFileHandler->FATBuffer[fat_offset * 4] = (value) & 0xff;
115 pFileHandler->FATBuffer[fat_offset * 4 + 1] = (value >> 8) & 0xff;
116 pFileHandler->FATBuffer[fat_offset * 4 + 2] = (value >> 16) & 0xff;
117 pFileHandler->FATBuffer[fat_offset * 4 + 3] = (value >> 24) & 0xff;
118 } else {
119 pFileHandler->FATBuffer[fat_offset * 2] = (value) & 0xff;
120 pFileHandler->FATBuffer[fat_offset * 2 + 1] = (value >> 8) & 0xff;
121 }
122}
123
124uint32_t FindBootPartition_sd(uint8_t *SectorBuffer)
125{
126 uint32_t PartitionStart;
127 uint32_t PartitionTpye;
128 PartitionTpye = BytesToNum_sd(SectorBuffer + 0x1c2, 1);
129 PartitionStart = BytesToNum_sd(SectorBuffer + 0x1c6, 4);
130
131 voprintf_debug("SDCard: PartitionStart=0x%x ,PartitionTpye=0x%x\n", PartitionStart, PartitionTpye);
132 if (PartitionTpye == 5) {
133 if (!fatfs_dev_read(dumpdev, PartitionStart,SectorBuffer,1)) {
134 voprintf_debug("SDCard: can not find BootPosition! \n");
135 return 0;
136 }
137 PartitionStart += BytesToNum_sd(SectorBuffer + 0x1c6, 4);
138 voprintf_debug("SDCard: PartitionStart=0x%x\n", PartitionStart);
139 }
140 return PartitionStart;
141}
142
143bool ReadBootPartition_sd(uint8_t *SectorBuffer)
144{
145 m_bFATInfo.BPB_BytsPerSec = BytesToNum_sd(SectorBuffer+11, 2);
146 m_bFATInfo.BPB_SecPerClus = BytesToNum_sd(SectorBuffer+13, 1);
147 m_bFATInfo.BPB_RsvdSecCnt = BytesToNum_sd(SectorBuffer+14, 2);
148 m_bFATInfo.BPB_NumFATs = BytesToNum_sd(SectorBuffer+16, 1);
149 m_bFATInfo.BPB_FATSz = BytesToNum_sd(SectorBuffer+22, 2);
150 if (m_bFATInfo.BPB_FATSz) {
151 m_bFATInfo.FileSysType = FAT_16;
152 m_bFATInfo.BPB_RootEntCnt = BytesToNum_sd(SectorBuffer+17, 2);
153 m_bFATInfo.BPB_TotSec = BytesToNum_sd(SectorBuffer+19, 2);
154 m_bFATInfo.BPB_RootClus = 0;
155 voprintf_debug("SDCard: FilSysType = FAT16\n");
156 } else {
157 m_bFATInfo.FileSysType = FAT_32;
158 m_bFATInfo.BPB_TotSec = BytesToNum_sd(SectorBuffer+32, 4);
159 m_bFATInfo.BPB_FATSz = BytesToNum_sd(SectorBuffer+36, 4);
160 m_bFATInfo.BPB_RootEntCnt = 0;
161 m_bFATInfo.BPB_RootClus = BytesToNum_sd(SectorBuffer+44, 4);
162 voprintf_debug("SDCard: FilSysType = FAT32\n");
163 }
164
165 voprintf_debug("SDCard: BPB_BytsPerSec = 0x%04x\n", m_bFATInfo.BPB_BytsPerSec);
166 voprintf_debug("SDCard: BPB_SecPerClus = 0x%02x\n", m_bFATInfo.BPB_SecPerClus);
167 voprintf_debug("SDCard: BPB_RsvdSecCnt = 0x%04x\n", m_bFATInfo.BPB_RsvdSecCnt);
168 voprintf_debug("SDCard: BPB_NumFATs = 0x%02x\n", m_bFATInfo.BPB_NumFATs);
169 voprintf_debug("SDCard: BPB_FATSz = 0x%08x\n", m_bFATInfo.BPB_FATSz);
170 voprintf_debug("SDCard: BPB_RootClus = 0x%08x\n", m_bFATInfo.BPB_RootClus);
171 voprintf_debug("SDCard: BPB_TotSec = 0x%08x\n", m_bFATInfo.BPB_TotSec);
172
173 if ((PAGE_SIZE > m_bFATInfo.BPB_SecPerClus * m_bFATInfo.BPB_BytsPerSec) ||
174 ((m_bFATInfo.BPB_SecPerClus * m_bFATInfo.BPB_BytsPerSec) % PAGE_SIZE != 0)) {
175 voprintf_error("Can't support SDCard cluster bytes %d\n", m_bFATInfo.BPB_SecPerClus * m_bFATInfo.BPB_BytsPerSec);
176 return false;
177 }
178 return true;
179}
180
181static bool Block0_is_BootSector(uint8_t *Ptr)
182{
183 uint32_t BytesPerSec = 0;
184 uint32_t SecPerClus = 0;
185 uint32_t BPB_Media = 0;
186
187 BytesPerSec = BytesToNum_sd(Ptr + 11, 2);
188 if (!((BytesPerSec == 512)||(BytesPerSec == 1024)||(BytesPerSec == 2048)||(BytesPerSec == 4096))) {
189 voprintf_error("Unsupport sector size %d\n", BytesPerSec);
190 return false;
191 }
192 SecPerClus = BytesToNum_sd(Ptr + 13, 1);
193 if (!((SecPerClus == 1)||(SecPerClus == 2)||(SecPerClus == 4)||
194 (SecPerClus == 8)||(SecPerClus == 16)||(SecPerClus == 32)||
195 (SecPerClus == 64)||(SecPerClus == 128)) &&
196 (SecPerClus * BytesPerSec <= 0x10000)) {
197 voprintf_error("Unsupport cluster size %d\n", SecPerClus);
198 return false;
199 }
200 BPB_Media = BytesToNum_sd(Ptr + 21, 1);
201 if (!((BPB_Media == 0xF8)||(BPB_Media == 0xF0)||(BPB_Media == 0xF9)||
202 (BPB_Media == 0xFA)||(BPB_Media == 0xFB)||(BPB_Media == 0xFC)||
203 (BPB_Media == 0xFD)||(BPB_Media == 0xFE)||(BPB_Media == 0xFF))) {
204 voprintf_error("Unsupport media descriptor %d\n", BPB_Media);
205 return false;
206 }
207
208 return true;
209}
210
211bool GetBPBInfo_sd(uint8_t *Ptr)
212{
213 m_bFATInfo.BPB_BytsPerSec = 512;
214 m_bFATInfo.BootStartSec = 0;
215
216 //*pSectorPosition
217 if (!fatfs_dev_read(dumpdev, m_bFATInfo.BootStartSec, Ptr, 1)) {
218 voprintf_error("SDCard: can not find MBR\n");
219 return false;
220 }
221
222 // Add support block0 is bootPartition
223 if (Block0_is_BootSector(Ptr)) {
224 m_bFATInfo.BootStartSec = 0;
225 } else {
226 m_bFATInfo.BootStartSec = FindBootPartition_sd(Ptr);
227 if (!fatfs_dev_read(dumpdev, m_bFATInfo.BootStartSec, Ptr, 1)) {
228 voprintf_error("SDCard: can't find BootPosition\n");
229 mrdump_status_error("SDCard: can't find BootPosition\n");
230 return false;
231 }
232 if (!Block0_is_BootSector(Ptr)) {
233 voprintf_error("SDCard: BPB sector dismatch FAT Spec\n");
234 mrdump_status_error("SDCard: BPB sector dismatch FAT Spec\n");
235 return false;
236 }
237 }
238 if (!ReadBootPartition_sd(Ptr)) {
239 voprintf_error("SDCard: can not Read BootPartition\n");
240 mrdump_status_error("SDCard: can not Read BootPartition\n");
241 return false;
242 }
243
244 m_bFATInfo.FATStartSec = m_bFATInfo.BootStartSec + m_bFATInfo.BPB_RsvdSecCnt;
245
246 if (m_bFATInfo.FileSysType == FAT_32) {
247 m_bFATInfo.ClusStartSec = m_bFATInfo.FATStartSec+(m_bFATInfo.BPB_NumFATs)*(m_bFATInfo.BPB_FATSz);
248 m_bFATInfo.RootDirStartSec = m_bFATInfo.ClusStartSec + (m_bFATInfo.BPB_RootClus-2)*(m_bFATInfo.BPB_SecPerClus);
249 } else {
250 m_bFATInfo.RootDirStartSec = m_bFATInfo.FATStartSec+(m_bFATInfo.BPB_NumFATs)*(m_bFATInfo.BPB_FATSz);
251 m_bFATInfo.ClusStartSec = m_bFATInfo.RootDirStartSec+32*m_bFATInfo.BPB_RootEntCnt/m_bFATInfo.BPB_BytsPerSec;
252 }
253
254 return true;
255}
256
257uint32_t FindFirstClusInFAT_sd(uint32_t StartClusNum, uint8_t *Ptr)
258{
259 uint32_t SectorNum;
260 uint32_t NextClusterPosition;
261 uint32_t BytsPerFAT;
262 uint32_t BytsPerAdd;
263 if (m_bFATInfo.FileSysType == FAT_32)
264 BytsPerAdd = 4;//FAT32
265 else
266 BytsPerAdd = 2;//FAT16
267
268 BytsPerFAT = m_bFATInfo.BPB_BytsPerSec/BytsPerAdd;
269
270 m_bLastFATPage = StartClusNum/BytsPerFAT;
271 SectorNum = m_bFATInfo.FATStartSec+m_bLastFATPage;
272#ifdef SD_DATA_PRINT
273 voprintf_verbose("SDCard: now FATSec = %d \n",SectorNum);
274#endif
275 if (!fatfs_dev_read(dumpdev, SectorNum, Ptr, 1)) {
276 voprintf_error("SDCard: can not FindFirstClusInFAT_sd\n");
277 // FIXME: this is not right, why return 0?
278 return 0;
279 }
280
281 NextClusterPosition = BytesToNum_sd(Ptr+BytsPerAdd * (StartClusNum%BytsPerFAT), BytsPerAdd);
282#ifdef SD_DATA_PRINT
283 voprintf_verbose("SDCard: NextClusterPosition = %08x\n", NextClusterPosition);
284#endif
285 return NextClusterPosition;
286}
287
288uint32_t FindNextClusInFAT_sd(uint32_t StartClusNum, uint8_t *Ptr)
289{
290 uint32_t SectorNum;
291 uint32_t NextClusterPosition;
292 uint32_t BytsPerFAT;
293 uint32_t BytsPerAdd;
294 uint32_t TempPage;
295 if (m_bFATInfo.FileSysType==FAT_32)
296 BytsPerAdd = 4;//FAT32
297 else
298 BytsPerAdd = 2;//FAT16
299
300 BytsPerFAT = m_bFATInfo.BPB_BytsPerSec/BytsPerAdd;
301 TempPage = StartClusNum/BytsPerFAT;
302 if (TempPage!=m_bLastFATPage) {
303 SectorNum = m_bFATInfo.FATStartSec+TempPage;
304#ifdef SD_DATA_PRINT
305 DBGKDUMP_PRINTK("SDCard: now FATSec = %d \n",SectorNum);
306#endif
307 if (!fatfs_dev_read(dumpdev, SectorNum,Ptr,1)) {
308 voprintf_error("SDCard: can not FindNextClusInFAT_sd! \n");
309 // FIXME: this is not right, why return 0?
310 return 0;
311 }
312 m_bLastFATPage=TempPage;
313#if 0
314 for (j=0; j<(int)m_bFATInfo.BPB_BytsPerSec; j++) {
315 if ((j%16)==0) DBGKDUMP_PRINTK("i= 0x%04x ",j);
316 DBGKDUMP_PRINTK(" %02x ", Ptr[j]);
317 if (((j+1)%16)==0) DBGKDUMP_PRINTK(" i= %d\n",j);
318 }
319#endif
320 }
321 NextClusterPosition=BytesToNum_sd(Ptr+BytsPerAdd*(StartClusNum%BytsPerFAT),BytsPerAdd);
322#ifdef SD_DATA_PRINT
323 DBGKDUMP_PRINTK("SDCard: NextClusterPosition = %08x \n",NextClusterPosition);
324#endif
325 return NextClusterPosition;
326}
327
328uint32_t FindFirstFreeClusInFAT_sd(FileHandler *pFileHandler)
329{
330 uint32_t i;
331 uint32_t SectorNum;
332 uint32_t FreeClusterNum;
333 uint32_t value;
334 uint32_t FATSector, FATOffset;
335 uint32_t EntryPerSector;
336 uint32_t BytsPerAdd;
337
338 if (m_bFATInfo.FileSysType==FAT_32)
339 BytsPerAdd = 4;//FAT32
340 else
341 BytsPerAdd = 2;//FAT16
342
343 FreeClusterNum = 0;
344 EntryPerSector = m_bFATInfo.BPB_BytsPerSec/BytsPerAdd;
345 FATSector = pFileHandler->CurrClusterNum/EntryPerSector;
346 FATOffset = pFileHandler->CurrClusterNum%EntryPerSector + 1; // start search frome next cluster
347
348 // for safty, we don't use all FAT entries, just reserve the last FAT sector
349 while (FATSector < (m_bFATInfo.BPB_FATSz-1)) {
350 // Read new FAT sector to cache
351 SectorNum = m_bFATInfo.FATStartSec+FATSector;
352 if (!fatfs_dev_read(dumpdev, SectorNum, pFileHandler->FATBuffer, 1)) {
353 voprintf_error("SDCard: %s read failed\n", __func__);
354 return 0;
355 }
356
357 for (i=FATOffset; i<EntryPerSector; i++) {
358 value = BytesToNum_sd(pFileHandler->FATBuffer+i*BytsPerAdd, BytsPerAdd);
359 if (value == 0) {
360 FreeClusterNum = FATSector*EntryPerSector + i; // found free entry in FAT
361 pFileHandler->FATSector = FATSector;
362 return FreeClusterNum;
363 }
364 }
365 // try next FAT sector
366 FATSector++;
367 FATOffset = 0;
368 }
369
370 return 0;
371}
372
373uint32_t ChainFreeClusInFAT_sd(FileHandler *pFileHandler)
374{
375 uint32_t i;
376 uint32_t SectorNum;
377 uint32_t FreeClusterNum;
378 uint32_t value;
379 uint32_t CurrFATSector, CurrFATOffset;
380 uint32_t NextFATSector, NextFATOffset;
381 uint32_t EntryPerSector;
382 uint32_t BytsPerAdd;
383 uint8_t TempFAT[512];
384 uint8_t *pBuf;
385
386 if (m_bFATInfo.FileSysType==FAT_32)
387 BytsPerAdd = 4;//FAT32
388 else
389 BytsPerAdd = 2;//FAT16
390
391 FreeClusterNum = 0;
392 EntryPerSector = m_bFATInfo.BPB_BytsPerSec/BytsPerAdd;
393 CurrFATSector = pFileHandler->CurrClusterNum/EntryPerSector;
394 CurrFATOffset = pFileHandler->CurrClusterNum%EntryPerSector;
395 NextFATSector = CurrFATSector;
396 NextFATOffset = CurrFATOffset+1; // start search frome next cluster
397 memset(TempFAT,0,512);
398
399 pBuf = pFileHandler->FATBuffer;
400 // for safty, we don't use all FAT entries, just reserve the last FAT sector
401 while (NextFATSector < (m_bFATInfo.BPB_FATSz-1)) {
402 if (NextFATSector != pFileHandler->FATSector) {
403 // FAT sector changed, read new FAT sector to temp buffer
404 pBuf = TempFAT;
405 SectorNum = m_bFATInfo.FATStartSec+NextFATSector;
406 if (!fatfs_dev_read(dumpdev, SectorNum, pBuf, 1)) {
407 voprintf_error("SDCard: %s read failed\n", __func__);
408 return 0;
409 }
410 }
411 // find free cluster in FAT cache
412 for (i=NextFATOffset; i<EntryPerSector; i++) {
413 value = BytesToNum_sd(pBuf+i*BytsPerAdd, BytsPerAdd);
414 if (value == 0) {
415 FreeClusterNum = NextFATSector*EntryPerSector + i; // found free entry in FAT
416 // commit FAT entry
417 fatfs_commit_fat_entry(pFileHandler, CurrFATOffset, FreeClusterNum);
418 break;
419 }
420 }
421
422 if (FreeClusterNum!=0) {
423 break;
424 }
425
426 // try next FAT sector
427 NextFATSector++;
428 NextFATOffset = 0;
429 }
430
431 // check if need to refresh cache
432 if ((FreeClusterNum != 0) && (NextFATSector != pFileHandler->FATSector)) {
433 // FAT sector changed, flush cache to SD
434 SectorNum = m_bFATInfo.FATStartSec+CurrFATSector;
435 if (!fatfs_dev_write(dumpdev, SectorNum, pFileHandler->FATBuffer, 1)) {
436 voprintf_error("SDCard: %s write failed\n", __func__);
437 return 0;
438 }
439
440 // copy temp buffer to cache
441 memcpy(pFileHandler->FATBuffer, TempFAT, 512);
442 pFileHandler->FATSector = NextFATSector;
443 }
444
445 return FreeClusterNum;
446}
447
448bool MarkEndClusInFAT_sd(FileHandler *pFileHandler)
449{
450 uint32_t SectorNum;
451 uint32_t BytsPerAdd;
452 uint32_t EntryPerSector;
453 uint32_t FATSector;
454 uint32_t FATOffset;
455
456 if (m_bFATInfo.FileSysType==FAT_32)
457 BytsPerAdd = 4;//FAT32
458 else
459 BytsPerAdd = 2;//FAT16
460
461 EntryPerSector = m_bFATInfo.BPB_BytsPerSec/BytsPerAdd;
462 FATSector = pFileHandler->CurrClusterNum/EntryPerSector;
463 FATOffset = pFileHandler->CurrClusterNum%EntryPerSector;
464 SectorNum = m_bFATInfo.FATStartSec+FATSector;
465
466 if (FATSector != pFileHandler->FATSector) {
467 if (!fatfs_dev_read(dumpdev, SectorNum, pFileHandler->FATBuffer, 1)) {
468 voprintf_error("SDCard: MarkEndClusInFAT_sd dumpdev->read failed\n");
469 return false;
470 }
471 }
472
473 fatfs_commit_fat_entry(pFileHandler, FATOffset, 0xffffffff);
474
475 // flush FAT cache to SD
476 if (!fatfs_dev_write(dumpdev, SectorNum, pFileHandler->FATBuffer, 1)) {
477 voprintf_error("SDCard: MarkEndClusInFAT_sd dumpdev->write failed\n");
478 return false;
479 }
480 return true;
481}
482
483bool DeleteFileInFAT_sd(FileHandler *pFileHandler)
484{
485 uint32_t StartClusNum;
486 uint32_t SectorNum;
487 uint32_t NextClusNum;
488 uint32_t FATSector, FATOffset;
489 uint32_t EntryPerSector;
490 uint32_t BytsPerAdd;
491 bool NewSector = true;
492 bool LastEntry = false;
493
494 if (m_bFATInfo.FileSysType == FAT_32) {
495 BytsPerAdd = 4;//FAT32
496 } else {
497 BytsPerAdd = 2;//FAT16
498 }
499
500 EntryPerSector = m_bFATInfo.BPB_BytsPerSec/BytsPerAdd;
501 StartClusNum = pFileHandler->CurrClusterNum;
502 SectorNum = m_bFATInfo.FATStartSec+StartClusNum/EntryPerSector;
503
504 while (!LastEntry) {
505 FATSector = StartClusNum/EntryPerSector;
506 FATOffset = StartClusNum%EntryPerSector;
507
508 if (NewSector) {
509 SectorNum = m_bFATInfo.FATStartSec+FATSector;
510#ifdef SD_DATA_PRINT
511 DBGKDUMP_PRINTK("SDCard: now FATSec = %d \n",SectorNum);
512#endif
513 if (!fatfs_dev_read(dumpdev, SectorNum, pFileHandler->FATBuffer, 1)) {
514 voprintf_error("SDCard: DeleteFile_sd read failed\n");
515 return false;
516 }
517 NewSector = false;
518 }
519
520 NextClusNum = BytesToNum_sd(pFileHandler->FATBuffer+FATOffset*BytsPerAdd, BytsPerAdd);
521
522 fatfs_commit_fat_entry(pFileHandler, FATOffset, 0x0);
523 // Release FAT entry
524 if (m_bFATInfo.FileSysType==FAT_32) {
525
526 if ((NextClusNum >= 0xFFFFFF8) || (NextClusNum == 0)) {
527 LastEntry = true;
528 }
529 } else {
530 if ((NextClusNum >= 0xFFF8) || (NextClusNum == 0)) {
531 LastEntry = true;
532 }
533 }
534
535 if (NextClusNum/EntryPerSector != FATSector) {
536 NewSector = true; // next cluster is not at current FAT Sector, write current FAT sector back
537 }
538
539 if (NewSector || LastEntry) {
540 if (!fatfs_dev_write(dumpdev, SectorNum, pFileHandler->FATBuffer, 1)) {
541 voprintf_error("SDCard: DeleteFile_sd write failed\n");
542 return false;
543 }
544 }
545
546 StartClusNum = NextClusNum;
547 }
548
549#ifdef SD_DATA_PRINT
550 DBGKDUMP_PRINTK("SDCard: NextClusterPosition = %08x \n",NextClusterPosition);
551#endif
552
553 return true;
554}
555
556static bool OpenDumpFile_sd(FileHandler *pFileHandler)
557{
558 int i, j, times;
559 uint32_t SectorNum;
560 uint32_t Temp;
561 uint32_t dwStartTick;
562 uint32_t FindFile_TIMEOUT = 60000;
563 int SecLen;
564 uint32_t NextRootFAT = 0;
565 uint8_t RootDirFAT[512];
566 bool foundLfn = false;
567
568 // init File Handler
569 memset(pFileHandler, 0, sizeof(FileHandler));
570 if (!GetBPBInfo_sd(pFileHandler->FileBuffer)) {
571 return false;
572 }
573
574 dwStartTick = OALGetTickCount();
575
576 voprintf_debug("SDCard: FATStartSec=%d RootdirStartSec=%d\n", m_bFATInfo.FATStartSec, m_bFATInfo.RootDirStartSec);
577 SectorNum = m_bFATInfo.RootDirStartSec;
578 if (m_bFATInfo.FileSysType == FAT_16) {
579 SecLen=32;
580 times=1;
581 } else {
582 SecLen = m_bFATInfo.BPB_SecPerClus;
583 times=MaxFindFileClusNum;
584 }
585
586 while (times) {
587 for (i = 0; i < SecLen; i++) {
588 if (!fatfs_dev_read(dumpdev, SectorNum, pFileHandler->FileBuffer, 1)) {
589 voprintf_error("SDCard: can not read RootDir!\n");
590 return false;
591 }
592
593 for (j=0; j<(int)m_bFATInfo.BPB_BytsPerSec; j+=32) {
594 if (foundLfn) {
595 Temp = BytesToNum_sd(pFileHandler->FileBuffer+j+20, 2);
596 pFileHandler->CurrClusterNum |= (Temp<<16);
597 Temp = BytesToNum_sd(pFileHandler->FileBuffer+j+26, 2);
598 pFileHandler->CurrClusterNum |= (Temp&0xFFFF);
599
600 // delete file DIR entries
601 memset(pFileHandler->FileBuffer+j, 0, 32);
602 if (!fatfs_dev_write(dumpdev, SectorNum, pFileHandler->FileBuffer, 1)) {
603 voprintf_error("SDCard: can not write directory entry!\n");
604 return false;
605 }
606 // delete file in FAT entries
607 if (!DeleteFileInFAT_sd(pFileHandler)) {
608 voprintf_error("SDCard: can not delete file in FAT entries!\n");
609 return false;
610 }
611 goto Done;
612 } else if (pFileHandler->FileBuffer[j] == 0x41 &&
613 Compare_sd(pFileHandler->FileBuffer+j+1, g_LfnEntry.name1, 10) &&
614 Compare_sd(pFileHandler->FileBuffer+j+14, g_LfnEntry.name2, 12)) {
615#if 0
616 // delete LFN entries
617 memset(pFileHandler->FileBuffer+j, 0, 32);
618 if (!fatfs_dev_write(dumpdev, SectorNum, pFileHandler->FileBuffer, 1)) {
619 voprintf_error("SDCard: can not write LFN entry!\n");
620 return false;
621 }
622 foundLfn = true;
623#else
624 voprintf_error("SDCard: dump file exist, skip dumping\n");
625 mrdump_status_error("File exist at SDCARD, skip dumping\n");
626 return false;
627#endif
628 }
629 }
630 SectorNum+=1;
631 }
632 if ((OALGetTickCount() - dwStartTick) >= FindFile_TIMEOUT) {
633 voprintf_error("SDCard: Find File timeout\n");
634 return false;
635 }
636 if (m_bFATInfo.FileSysType==FAT_32) {
637 if (times==MaxFindFileClusNum) {
638 NextRootFAT=FindFirstClusInFAT_sd(m_bFATInfo.BPB_RootClus, RootDirFAT);
639 if (NextRootFAT>=0xFFFFFF8) {
640 break;
641 }
642 } else {
643 NextRootFAT=FindNextClusInFAT_sd(NextRootFAT, RootDirFAT);
644 if (NextRootFAT >= 0xFFFFFF8) {
645 break;
646 }
647 }
648 voprintf_info("SDCard: NextRootFAT=0x%08x \n", NextRootFAT);
649 SectorNum=m_bFATInfo.ClusStartSec + (NextRootFAT-2)*(m_bFATInfo.BPB_SecPerClus);
650 } else {
651 // FAT16
652 break;
653 }
654 times--;
655 }
656
657Done:
658 // return a free cluster to create new file
659 pFileHandler->CurrClusterNum = 2;
660 pFileHandler->CurrClusterNum = FindFirstFreeClusInFAT_sd(pFileHandler);
661 pFileHandler->PrevClusterNum = pFileHandler->CurrClusterNum;
662 g_DirEntry.clusFirstHigh = (uint16_t)((pFileHandler->CurrClusterNum >> 16) & 0xFFFF);
663 g_DirEntry.clusFirst = (uint16_t)(pFileHandler->CurrClusterNum & 0xFFFF);
664 if (!pFileHandler->CurrClusterNum) {
665 pFileHandler->DiskFull = true;
666 voprintf_error("SDCard full, not free space available at create file\n");
667 mrdump_status_error("SDCard full, not free space available at create file\n");
668 return false;
669 }
670
671 voprintf_debug("%s: ok\n", __func__);
672 return true;
673}
674
675bool UpdateDirectoryEntry_sd(FileHandler *pFileHandler)
676{
677 int i, j, times;
678 uint32_t SectorNum;
679 uint32_t dwStartTick;
680 uint32_t FindFile_TIMEOUT = 60000;
681 int SecLen;
682 uint32_t NextRootFAT = 0;
683 uint8_t RootDirFAT[512];
684
685 dwStartTick = OALGetTickCount();
686 voprintf_info("SDCard: FATStartSec = %d \n",m_bFATInfo.FATStartSec);
687 voprintf_info("SDCard: RootDirStartSec = %d \n", m_bFATInfo.RootDirStartSec);
688 SectorNum = m_bFATInfo.RootDirStartSec;
689
690 if (m_bFATInfo.FileSysType==FAT_16) {
691 SecLen=32;
692 times=1;
693 } else {
694 SecLen = m_bFATInfo.BPB_SecPerClus;
695 times=MaxFindFileClusNum;
696 }
697
698 while (times) {
699 for (i = 0; i < SecLen; i++) {
700 if (!fatfs_dev_read(dumpdev, SectorNum, pFileHandler->FileBuffer, 1)) {
701 voprintf_error("SDCard: can not read RootDir!\n");
702 return false;
703 }
704
705 for (j = 0; j < (int)(m_bFATInfo.BPB_BytsPerSec - 32); j += 32) {
706 if ((pFileHandler->FileBuffer[j] == 0x0 || pFileHandler->FileBuffer[j] == 0xE5) &&
707 (pFileHandler->FileBuffer[j+32] == 0x0 || pFileHandler->FileBuffer[j+32] == 0xE5)) {
708 memcpy(pFileHandler->FileBuffer+j, &g_LfnEntry, sizeof(LfnEntry));
709 memcpy(pFileHandler->FileBuffer+j+32, &g_DirEntry, sizeof(DirEntry));
710
711 if (!fatfs_dev_write(dumpdev, SectorNum, pFileHandler->FileBuffer, 1)) {
712 voprintf_error("SDCard: can not read RootDir! \n");
713 return false;
714 }
715 return true;
716 }
717 }
718 SectorNum += 1;
719 }
720 if ((OALGetTickCount() - dwStartTick) >= FindFile_TIMEOUT) {
721 voprintf_error("SDCard: Find File Error timeout\n");
722 return false;
723 }
724 if (m_bFATInfo.FileSysType==FAT_32) {
725 if (times == MaxFindFileClusNum) {
726 NextRootFAT=FindFirstClusInFAT_sd(m_bFATInfo.BPB_RootClus, RootDirFAT);
727 if (NextRootFAT>=0xFFFFFF8) {
728 voprintf_error("SDCard: Dump file not exsited.\n");
729 break;
730 }
731 } else {
732 NextRootFAT=FindNextClusInFAT_sd(NextRootFAT, RootDirFAT);
733 if (NextRootFAT>=0xFFFFFF8) {
734 voprintf_error("SDCard: Dump file not exsited.\n");
735 break;
736 }
737 }
738 voprintf_error("SDCard: NextRootFAT=0x%08x \n", NextRootFAT);
739 SectorNum=m_bFATInfo.ClusStartSec + (NextRootFAT-2)*(m_bFATInfo.BPB_SecPerClus);
740 } else {
741 // FAT16
742 break;
743 }
744 times--;
745 }
746
747 return false;
748}
749
750static bool WriteDumpFile_sd(FileHandler *pFileHandler, uint8_t *Ptr, uint32_t Length, uint32_t Total)
751{
752 uint32_t i;
753 uint32_t SectorNum;
754 uint32_t FreeClusterNum;
755 uint8_t val;
756
757 uint32_t ClusterSize = m_bFATInfo.BPB_SecPerClus * m_bFATInfo.BPB_BytsPerSec;
758
759 while (Length > 0) {
760 // for every cluster boundary, check disk free space
761 if (pFileHandler->DiskFull) {
762 return false;
763 }
764
765 for (i = pFileHandler->BufferLen; (i < ClusterSize) && (Length > 0); i++) {
766 val = *Ptr++;
767 pFileHandler->FileBuffer[pFileHandler->BufferLen++] = val;
768 pFileHandler->CheckSum += val;
769 pFileHandler->TotalLen++;
770 Length--;
771 }
772
773 // a cluster collected, flush to SD
774 if (pFileHandler->BufferLen == ClusterSize) {
775 pFileHandler->BufferLen = 0;
776 SectorNum=m_bFATInfo.ClusStartSec + (pFileHandler->CurrClusterNum-2)*(m_bFATInfo.BPB_SecPerClus);
777 //DBGKDUMP_PRINTK("SDCard: WriteDumpFile_sd() write sd card from %d blocks!\n",SectorNum);
778
779 if (!fatfs_dev_write(dumpdev, SectorNum, pFileHandler->FileBuffer, m_bFATInfo.BPB_SecPerClus)) {
780 voprintf_error("SDCard: WriteDumpFile_sd() write file content from %d blocks failed!!!!\n",SectorNum);
781 return false;
782 }
783
784 pFileHandler->PrevClusterNum = pFileHandler->CurrClusterNum;
785 FreeClusterNum = ChainFreeClusInFAT_sd(pFileHandler);
786 if (FreeClusterNum == 0) {
787 pFileHandler->DiskFull = true;
788 voprintf_error("SDcard full, no free space available\n");
789 mrdump_status_error("SDcard full, no free space available\n");
790 return false;
791 }
792 pFileHandler->CurrClusterNum =FreeClusterNum;
793 } else if (pFileHandler->BufferLen > ClusterSize) {
794 voprintf_error("SDCard: WriteDumpFile_sd() BufferLen error!\n");
795 }
796 }
797
798 return true;
799}
800
801static bool CloseDumpFile_sd(FileHandler *pFileHandler)
802{
803 uint32_t SectorNum;
804
805 g_DirEntry.size = pFileHandler->TotalLen;
806
807#if 0
808 uint16_t year;
809 g_DirEntry.createdTime = g_DirEntry.time = (((INREG16(&m_pRTCRegs->RTC_TC_HOU) << 11) & 0xF800) |
810 ((INREG16(&m_pRTCRegs->RTC_TC_MIN) << 5) & 0x7E0) |
811 ((INREG16(&m_pRTCRegs->RTC_TC_SEC)>>1) & 0x1F));
812 year = INREG16(&m_pRTCRegs->RTC_TC_YEA);
813 year = (year > 20) ? (year-20) : 0;
814 g_DirEntry.createdDate = g_DirEntry.date = (((year<<9) & 0xFE00) |
815 ((INREG16(&m_pRTCRegs->RTC_TC_MTH)<<5) & 0x1E0) |
816 (INREG16(&m_pRTCRegs->RTC_TC_DOM) & 0x1F));
817#endif
818 g_DirEntry.createdTime = g_DirEntry.time = 0;
819 g_DirEntry.createdDate = g_DirEntry.date = 0;
820
821 if (pFileHandler->BufferLen == 0) {
822 // the free cluster is not used, so that the last cluster should be the previous one
823 pFileHandler->CurrClusterNum = pFileHandler->PrevClusterNum;
824 } else if (!pFileHandler->DiskFull) {
825 // flush the reset data
826 SectorNum=m_bFATInfo.ClusStartSec + (pFileHandler->CurrClusterNum-2)*(m_bFATInfo.BPB_SecPerClus);
827 if (!fatfs_dev_write(dumpdev, SectorNum, pFileHandler->FileBuffer, m_bFATInfo.BPB_SecPerClus)) {
828 voprintf_error("SDCard: CloseFile_sd() write file content failed!\n");
829 return false;
830 }
831 }
832
833 if (!MarkEndClusInFAT_sd(pFileHandler)) {
834 voprintf_error("SDCard: CloseFile_sd() MarkEndClusInFAT_sd failed!\n");
835 return false;
836 }
837
838 if (!UpdateDirectoryEntry_sd(pFileHandler)) {
839 voprintf_error("SDCard: CloseFile_sd() UpdateDirectoryEntry_sd failed!\n");
840 return false;
841 }
842
843 return true;
844}
845
846static int sd_write_cb(void *handle, void *buf, int size)
847{
848 if (WriteDumpFile_sd(handle, buf, size, 0)) {
849 return size;
850 } else {
851 return 0;
852 }
853}
854
855int mrdump_vfat_output(const struct mrdump_control_block *mrdump_cb, const struct kzip_addlist *memlist, struct mrdump_dev *mrdump_dev)
856{
857 if (mrdump_dev == NULL) {
858 return -1;
859 }
860
861 dumpdev = mrdump_dev;
862 voprintf_info("Output to VFAT Partition %s\n", dumpdev->name);
863
864 FileHandler *file_handle = memalign(16, sizeof(FileHandler));
865 if (file_handle == NULL) {
866 voprintf_error("No enough memory.");
867 return -1;
868 }
869 memset(file_handle, 0, sizeof(FileHandler));
870
871 mtk_wdt_restart();
872 bool ok = true;
873 if (OpenDumpFile_sd(file_handle)) {
874 mtk_wdt_restart();
875 struct kzip_file *zf = kzip_open(file_handle, sd_write_cb);
876 if (zf != NULL) {
877 if (!kzip_add_file(zf, memlist, "SYS_COREDUMP")) {
878 ok = false;
879 }
880 kzip_close(zf);
881 zf = NULL;
882 } else {
883 ok = false;
884 }
885
886 mtk_wdt_restart();
887 CloseDumpFile_sd(file_handle);
888 free(file_handle);
889
890 if (ok) {
891 mrdump_status_ok("OUTPUT:%s\nMODE:%s\n", "VFAT_INT_STORAGE", mrdump_mode2string(mrdump_cb->crash_record.reboot_mode));
892 }
893 }
894
895 return ok ? 0 : -1;
896}
897