📄 fat.lst
字号:
210 1 end_offset=128;
211 1 else
212 1 end_offset=256;
213 1
214 1 while(1){
215 2 while(clus_fat_offset<end_offset){
216 3 if(deviceinfo.type){
217 4 if(!free_fat_item.fat32[clus_fat_offset]){
218 5 find_flag=1;
219 5 break;
220 5 }
221 4 }
222 3 else{
223 4 if(!free_fat_item.fat16[clus_fat_offset]){
224 5 find_flag=1;
225 5 break;
226 5 }
227 4 }
228 3 clus_fat_offset++;
229 3 }
230 2 if(find_flag) {
231 3 fat_info.next_free_clus=GetClusterNum(clus_fat_offset,fat_info.free_fat_sec);
232 3 return TRUE;
233 3 }
234 2 if(!updata_fat_free()) return FALSE;
235 2 fat_info.free_fat_sec++;
236 2 if(fat_info.free_fat_sec>deviceinfo.FATFirstSec+deviceinfo.FATSz){
237 3 fat_info.free_fat_sec=deviceinfo.FATFirstSec;
238 3 loop_time++;
239 3 if(loop_time>1) return FALSE;
240 3 }
241 2 if(!RBC_Read(fat_info.free_fat_sec,1,free_fat_item.date))
C51 COMPILER V7.00 FAT 09/22/2005 17:15:32 PAGE 5
242 2 return FALSE;
243 2 clus_fat_offset=0;
244 2 }
245 1 return FALSE;
246 1 }
247
248 /*******************************************************************
249 将当前簇与空闲簇形成簇链
250 该函数未保存FAT表信息,调用函数保存
251 *******************************************************************/
252 uchar CreateClusterLink(ulong currentCluster)
253 {
254 1 uint fat_offset,free_offset;
255 1 ulong fat_sec_num;
256 1
257 1
258 1 fat_sec_num=ThisFatSecNum(currentCluster);
259 1 fat_offset=ThisFatEntOffset(currentCluster);
260 1
261 1 if(fat_info.cur_fat_sec!=fat_sec_num){
262 2 if(!updata_fat_cur()) return FALSE;
263 2 if(!RBC_Read(fat_sec_num,1,FATBUF.date))
264 2 return FALSE;
265 2 fat_info.cur_fat_sec=fat_sec_num;
266 2 }
267 1 if(deviceinfo.type){
268 2 FATBUF.fat32[fat_offset]=SwapINT32(fat_info.next_free_clus);
269 2 free_fat_item.fat32[free_offset]=0x0fffffff;
270 2 }else{
271 2 FATBUF.fat16[fat_offset]=SwapINT16((uint)fat_info.next_free_clus);
272 2 free_fat_item.fat16[free_offset]=0xffff;
273 2 }
274 1 fat_info.free_fat_updata=1;
275 1 //更新空簇结构
276 1 if(!GetFreeCusterNum()) return FALSE;
277 1 return TRUE;
278 1 }
279
280 /********************************************************************
281 当前FAT表更新
282 ********************************************************************/
283 uchar updata_fat_cur(void)
284 {
285 1 if(fat_info.cur_fat_updata){
286 2 if(!RBC_Write(fat_info.cur_fat_sec,1,FATBUF.date))
287 2 return FALSE;
288 2 if(!RBC_Write(fat_info.cur_fat_sec+deviceinfo.FATSz,1,FATBUF.date))
289 2 return FALSE;
290 2 fat_info.cur_fat_updata=0;
291 2 if(fat_info.cur_fat_sec==fat_info.free_fat_sec){
292 3 memcpy_cur(free_fat_item.date,FATBUF.date,512);
293 3 }
294 2 }
295 1 return TRUE;
296 1 }
297
298 /********************************************************************
299 空簇FAT表更新
300 ********************************************************************/
301 uchar updata_fat_free(void)
302 {
303 1 if(fat_info.free_fat_updata){
C51 COMPILER V7.00 FAT 09/22/2005 17:15:32 PAGE 6
304 2 if(!RBC_Write(fat_info.free_fat_sec,1,free_fat_item.date))
305 2 return FALSE;
306 2 if(!RBC_Write(fat_info.free_fat_sec+deviceinfo.FATSz,1,free_fat_item.date))
307 2 return FALSE;
308 2 fat_info.free_fat_updata=0;
309 2 if(fat_info.cur_fat_sec==fat_info.free_fat_sec){
310 3 memcpy_cur(FATBUF.date, free_fat_item.date, 512);
311 3 }
312 2 }
313 1 return TRUE;
314 1 }
315
316 /*********************************************************************
317 初始化空簇FAT表
318 *********************************************************************/
319 uchar init_free_cur(void)
320 {
321 1 uchar find_flag=0,error;
322 1 uint free_offset;
323 1
324 1 //FAT32
325 1 if(deviceinfo.type){
326 2 fat_info.cur_fat_sec=deviceinfo.FSInfoSec;
327 2 if(!RBC_Read(fat_info.cur_fat_sec, 1,FATBUF.date)) return DEVICEC_OP_ERR;
328 2 if((FATBUF.fat32[0]==0x52526141)&&(FATBUF.fat32[121]==0x72724161)){
329 3 //FSINFO扇区有效
330 3 if(FATBUF.fat32[122]!=0xffffffff){
331 4 fat_info.free_count=SwapINT32(FATBUF.fat32[122]);
332 4 }
333 3 if(FATBUF.fat32[123]!=0xffffffff){
334 4 fat_info.next_free_clus=SwapINT32(FATBUF.fat32[123]);
335 4 }
336 3 fat_info.free_fat_sec=ThisFatSecNum(fat_info.next_free_clus);
337 3 free_offset=ThisFatEntOffset(fat_info.next_free_clus);
338 3 if(!RBC_Read(fat_info.free_fat_sec,1,free_fat_item.date)) return DEVICEC_OP_ERR;
339 3 if(!free_fat_item.fat32[free_offset]){
340 4 find_flag=1;
341 4 return 0;
342 4 }
343 3 }
344 2 }
345 1 else{
346 2 fat_info.free_count=0;
347 2 fat_info.cur_fat_sec=0;
348 2 }
349 1 //查找空簇
350 1 error=search_free_cur();
351 1 if((error)&&(error!=NOT_SEEK)) return error;
352 1
353 1 if(error==NOT_SEEK){
354 2 error=search_one_free();
355 2 if(error) return error;
356 2 }
357 1 return 0;
358 1
359 1 }
360
361 /*********************************************************************
362 二分法查找空簇
363 *********************************************************************/
364 uchar search_free_cur(void)
365 {
C51 COMPILER V7.00 FAT 09/22/2005 17:15:32 PAGE 7
366 1 ulong start_sec,end_sec,mid_sec;
367 1 uchar empty,full,find_flag=0;
368 1 uint i,end_offset;
369 1
370 1 start_sec=deviceinfo.FATFirstSec;
371 1 end_sec=deviceinfo.FATFirstSec+deviceinfo.FATSz-1;
372 1
373 1 while(start_sec!=end_sec){
374 2 empty=0;
375 2 full=0;
376 2 //得到中间值
377 2 mid_sec=(end_sec-start_sec)/2+start_sec;
378 2 if(!RBC_Read(mid_sec,1,free_fat_item.date)) return DEVICEC_OP_ERR;
379 2 if(deviceinfo.type){
380 3 if(free_fat_item.fat32[0]){
381 4 empty=1;
382 4 }
383 3 else{
384 4 full=1;
385 4 }
386 3 if(free_fat_item.fat32[127]){
387 4 empty=1;
388 4 }
389 3 else{
390 4 full=1;
391 4 }
392 3 }
393 2 else{
394 3 if(free_fat_item.fat16[0]){
395 4 empty=1;
396 4 }
397 3 else{
398 4 full=1;
399 4 }
400 3 if(free_fat_item.fat16[255]){
401 4 empty=1;
402 4 }
403 3 else{
404 4 full=1;
405 4 }
406 3 }
407 2 //中间点在该扇区
408 2 if(empty&&full){
409 3 find_flag=1;
410 3 break;
411 3 }
412 2 //向前查找
413 2 if(empty){
414 3 if(end_sec==mid_sec){
415 4 //查找到最后点
416 4 start_sec=mid_sec;
417 4 }else{
418 4 end_sec=mid_sec;
419 4 }
420 3 }
421 2 //向后查找
422 2 else{
423 3 if(start_sec==mid_sec){
424 4 //查找到最后点
425 4 end_sec=mid_sec;
426 4 }else{
427 4 start_sec=mid_sec;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -