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