📄 fat.lst
字号:
247 2 return KO;
248 2 }
249 1
250 1 /* read and check usefull PBR info */
251 1 if (fat_load_sector(fat_ptr_fats) == OK)
252 1 {
253 2 if ((fat_buf_sector[11] != LOW(SECTOR_SIZE)) || /* read sector size (in bytes) */
254 2 (fat_buf_sector[12] != HIGH(SECTOR_SIZE)))
255 2 {
256 3 return KO;
257 3 }
258 2
259 2 /* read cluster size (in sector) */
260 2 fat_cluster_size = fat_buf_sector[13];
261 2 fat_cluster_mask = HIGH((Uint16)fat_cluster_size * SECTOR_SIZE) - 1;
262 2 /* compute FATs sector address: add reserved sector number */
263 2 fat_ptr_fats += fat_buf_sector[14];
264 2 fat_ptr_fats += (Uint16)fat_buf_sector[15] << 8;
265 2 /* read number of FATs */
266 2 if (fat_buf_sector[16] == 2)
267 2 fat_2_is_present = TRUE;
268 2 else
269 2 fat_2_is_present = FALSE;
270 2 /* read number of dir entries and compute rdir offset */
271 2 ((Byte*)&fat_ptr_data)[3] = fat_buf_sector[17];
272 2 ((Byte*)&fat_ptr_data)[2] = fat_buf_sector[18];
273 2 ((Byte*)&fat_ptr_data)[1] = 0;
274 2 ((Byte*)&fat_ptr_data)[0] = 0;
275 2 fat_ptr_data = (fat_ptr_data * DIR_SIZE) / SECTOR_SIZE;
276 2 /* read number of sector in partition (<32Mb) */
277 2 ((Byte*)&fat_nb_sector)[3] = fat_buf_sector[19];
278 2 ((Byte*)&fat_nb_sector)[2] = fat_buf_sector[20];
279 2 ((Byte*)&fat_nb_sector)[1] = 0x00;
280 2 ((Byte*)&fat_nb_sector)[0] = 0x00;
281 2 /* compute root directory sector address */
282 2 ((Byte*)&fat_fat_size)[1] = fat_buf_sector[22];
283 2 ((Byte*)&fat_fat_size)[0] = fat_buf_sector[23];
284 2
285 2 fat_ptr_rdir = fat_buf_sector[16] * fat_fat_size;
286 2 fat_ptr_rdir += fat_ptr_fats;
287 2
288 2 /* read number of sector in partition (>32Mb) */
289 2 if (!fat_nb_sector)
290 2 {
291 3 ((Byte*)&fat_nb_sector)[3] = fat_buf_sector[32];
292 3 ((Byte*)&fat_nb_sector)[2] = fat_buf_sector[33];
293 3 ((Byte*)&fat_nb_sector)[1] = fat_buf_sector[34];
294 3 ((Byte*)&fat_nb_sector)[0] = fat_buf_sector[35];
295 3 }
296 2
297 2 fat_count_of_clusters = (fat_nb_sector - (1 + (fat_buf_sector[16] * fat_fat_size) + fat_ptr_data))
298 2 / fat_cluster_size;
299 2 if (fat_count_of_clusters <= MAX_CLUSTERS12)
300 2 fat_is_fat16 = FALSE;
301 2 else
302 2 if (fat_count_of_clusters <= MAX_CLUSTERS16)
C51 COMPILER V7.50 FAT 09/20/2005 21:47:47 PAGE 6
303 2 fat_is_fat16 = TRUE;
304 2 /* else is FAT32 not supported */
305 2
306 2 /* compute data sector address */
307 2 fat_ptr_data += fat_ptr_rdir;
308 2 /* check partition signature */
309 2 if ((fat_buf_sector[510] != LOW(BR_SIGNATURE)) &&
310 2 (fat_buf_sector[511] != HIGH(BR_SIGNATURE)))
311 2 {
312 3 return KO;
313 3 }
314 2
315 2 return OK;
316 2 }
317 1 else
318 1 { /* low level error */
319 2 return KO;
320 2 }
321 1 }
322
323
324 /*F**************************************************************************
325 * NAME: fat_get_dir_entry
326 *----------------------------------------------------------------------------
327 * PARAMS:
328 * entry: directory entry structure
329 *
330 * return:
331 *----------------------------------------------------------------------------
332 * PURPOSE:
333 * Get from directory all information about a directory or file entry
334 *----------------------------------------------------------------------------
335 * EXAMPLE:
336 *----------------------------------------------------------------------------
337 * NOTE:
338 * This function reads directly datas from sectors
339 * It automaticaly computes difference between LFN and normal entries
340 *----------------------------------------------------------------------------
341 * REQUIREMENTS:
342 *****************************************************************************/
343 void fat_get_dir_entry (fat_st_dir_entry xdata *entry)
344 {
345 1 bit exit_flag = FALSE;
346 1 bit lfn_entry_found = FALSE;
347 1 Byte i;
348 1
349 1 /* clear the name buffer */
350 1 for (i = MAX_FILENAME_LEN; i != 0; lfn_name[--i] = '\0');
351 1
352 1 while (!exit_flag)
353 1 /* loop while the entry is not a normal one. */
354 1 {
355 2 /* read the directory entry */
356 2 if (dir_is_root == TRUE)
357 2 { /* root dir is linear -> Hard_read_byte() */
358 3 for (i = 0; i < DIR_SIZE; i++)
359 3 gl_buffer[i] = Hard_read_byte();
360 3 }
361 2 else
362 2 { /* subdir can be fragmented -> dgetc() */
363 3 for (i = 0; i < DIR_SIZE; i++)
364 3 gl_buffer[i] = fat_dgetc();
C51 COMPILER V7.50 FAT 09/20/2005 21:47:47 PAGE 7
365 3 }
366 2
367 2 /*computes gathered data
368 2 /* check if we have a LFN entry */
369 2 if (gl_buffer[11] != ATTR_LFN_ENTRY)
370 2 {
371 3 if (!lfn_entry_found)
372 3 {
373 4 /* true DOS 8.3 entry format */
374 4 for (i = 0; i < 8; i++)
375 4 {
376 5 lfn_name[i] = gl_buffer[i];
377 5
378 5 if (lfn_name[i] == ' ')
379 5 { /* space is end of name */
380 6 break;
381 6 }
382 5 }
383 4 /* append extension */
384 4 lfn_name[i++] = '.';
385 4 lfn_name[i++] = gl_buffer[8];
386 4 lfn_name[i++] = gl_buffer[9];
387 4 lfn_name[i++] = gl_buffer[10];
388 4
389 4 for (; i != 14; i++)
390 4 {
391 5 lfn_name[i] = ' '; /* append spaces for display reason */
392 5 }
393 4 lfn_name[i] = '\0'; /* end of string */
394 4 }
395 3
396 3 else
397 3 { /* LFN name treatment */
398 4 i = 0;
399 4 /* search for the end of the string */
400 4 while (lfn_name[i] != '\0')
401 4 {
402 5 i++;
403 5 }
404 4 if (i <= 14)
405 4 { /* append spaces for display reason (no scrolling) */
406 5 while (i != 14)
407 5 {
408 6 lfn_name[i++] = ' ';
409 6 }
410 5 }
411 4 else
412 4 { /* append beginning of name to ease scrolling display */
413 5 lfn_name[i++] = ' ';
414 5 lfn_name[i++] = ' ';
415 5 lfn_name[i++] = lfn_name[0];
416 5 lfn_name[i++] = lfn_name[1];
417 5 lfn_name[i++] = lfn_name[2];
418 5 lfn_name[i++] = lfn_name[3];
419 5 lfn_name[i++] = lfn_name[4];
420 5 lfn_name[i++] = lfn_name[5];
421 5 lfn_name[i++] = lfn_name[6];
422 5 lfn_name[i++] = lfn_name[7];
423 5 lfn_name[i++] = lfn_name[8];
424 5 lfn_name[i++] = lfn_name[9];
425 5 lfn_name[i++] = lfn_name[10];
426 5 lfn_name[i++] = lfn_name[11];
C51 COMPILER V7.50 FAT 09/20/2005 21:47:47 PAGE 8
427 5 lfn_name[i++] = lfn_name[12];
428 5 }
429 4 lfn_name[i] = '\0'; /* end of name */
430 4 }
431 3
432 3 /* store extension */
433 3 ext[0]= gl_buffer[8];
434 3 ext[1]= gl_buffer[9];
435 3 ext[2]= gl_buffer[10];
436 3
437 3 /* standard computing for normal entry */
438 3 entry->attributes = gl_buffer[11];
439 3 entry->start_cluster = gl_buffer[26];
440 3 entry->start_cluster += ((Uint16) gl_buffer[27]) << 8;
441 3 entry->size.b[3] = gl_buffer[28];
442 3 entry->size.b[2] = gl_buffer[29];
443 3 entry->size.b[1] = gl_buffer[30];
444 3 entry->size.b[0] = gl_buffer[31];
445 3 /* now it's time to stop */
446 3 exit_flag = TRUE;
447 3 }
448 2 else
449 2 { /* LFN entry format */
450 3 lfn_entry_found = TRUE; /* a 8.3 name will follow */
451 3
452 3 if ((gl_buffer[0] & LFN_SEQ_MASK) <= MAX_LFN_ENTRIES)
453 3 { /* Maximum number of entries for LFN? */
454 4 for (i = 0; i < 5; i++)
455 4 lfn_name[i + 13*((gl_buffer[0] & LFN_SEQ_MASK) - 1)] = gl_buffer[2*i + 1];
456 4 for (i = 0; i < 6; i++)
457 4 lfn_name[i + 5 + 13*((gl_buffer[0] & LFN_SEQ_MASK) - 1)] = gl_buffer[2*i + 14];
458 4 for (i = 0; i < 2; i++)
459 4 lfn_name[i + 11 + 13*((gl_buffer[0] & LFN_SEQ_MASK) - 1)] = gl_buffer[2*i + 28];
460 4 }
461 3 }
462 2 }
463 1 Hard_read_close(); /* close physical read */
464 1 }
465
466
467 /*F**************************************************************************
468 * NAME: fat_get_dir_file_list
469 *----------------------------------------------------------------------------
470 * PARAMS:
471 * id: file extension to select
472 *
473 * return:
474 *----------------------------------------------------------------------------
475 * PURPOSE:
476 * Construct the file directory list
477 *----------------------------------------------------------------------------
478 * EXAMPLE:
479 *----------------------------------------------------------------------------
480 * NOTE:
481 * The value are relative position with the previous file.
482 * Call to this function assume that the dir fragment chain has been created
483 *----------------------------------------------------------------------------
484 * REQUIREMENTS:
485 * Maximum of 256 entries between 2 authorized file (id extension)
486 * because the relative position is stored on one byte.
487 * To allow more than 256 entries (-> 32768), change type of
488 * fat_dir_entry_list[] from Byte to Uint16 (no overflow management)
C51 COMPILER V7.50 FAT 09/20/2005 21:47:47 PAGE 9
489 * and change MAX_DIRECTORY_GAP_FILE from 255 to 32767
490 *****************************************************************************/
491 void fat_get_dir_file_list (Byte id)
492 {
493 1 Uint16 index; /* chain index */
494 1 Uint16 counter_entry; /* entry counter: 0..MAX_DIRECTORY_FILE */
495 1 Uint16 entry_pos; /* relative entry position */
496 1 Uint16 entry_pos_saved; /* used when the file is not the id etension */
497 1 Byte i;
498 1
499 1 index = 0;
500 1 fat_dir_list_last = 0;
501 1 counter_entry = 0;
502 1 entry_pos = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -