Código (c) [Seleccionar]
int fat32_open_file(uint8_t* filename, uint8_t* buff)
{
if (!hd_exists() && !filename)
return 1;
hd_read(start_of_root, FAT32_FILES_PER_DIRECTORY * sizeof(struct DirectoryEntry), (uint8_t*)&drce[0]);
for (int i = 0; i < FAT32_FILES_PER_DIRECTORY; ++i) {
if (drce[i].file_name[0] == FAT32_NO_FILES)
break;
uint8_t* fatbuff = 0;
uint8_t fil[12];
fat2human(drce[i].file_name, fil);
trimName(fil, 11);
if (strcmp((char*)fil, (char*)filename) == 0) {
uint8_t fcluster = ((uint32_t)drce[i].cluster_number_hi) << 16 | ((uint32_t)drce[i].cluster_number_lo);
int32_t ncluster = fcluster;
int32_t file_size = drce[i].file_size;
kputs("\nFile content: \n");
/* 1 sector file (less than 512 bytes) */
if (file_size < 512) {
hd_read(fcluster, 512, buff);
buff[file_size] = '\0';
//kputs("%s", (char*)buff);
}
while (file_size > 0) {
uint32_t fsect = start_of_data + bpb.sectors_per_cluster * (ncluster - 2);
uint32_t sector_offset = 0;
for (; file_size > 0; file_size -= 512) {
hd_read(fsect + sector_offset, 512, buff);
buff[file_size > 512 ? 512 : file_size] = '\0';
//kputs("%s", (char*)buff);
if (++sector_offset > bpb.sectors_per_cluster)
break;
}
uint32_t fsectcurrentcl = ncluster / (512 / sizeof(uint32_t));
hd_read(fat_start + fsectcurrentcl, 512, fatbuff);
uint32_t foffsectcurrentcl = ncluster % (512 / sizeof (uint32_t));
ncluster = ((uint32_t*)&fatbuff)[foffsectcurrentcl] & 0x0FFFFFFF;
}
return 0;
}
}
kputs("\nFile %s not found\n", filename);
return 1;
}
Funciona, pero hay un problema. Debo darle un tamaño al buffer:
Código (c) [Seleccionar]
uint8_t fileb[1024];
fat32_open_file(buff, fileb);
La cosa es que no se puede saber el tamaño del buffer pasado por la función, puede ser de 512, 2048, etc.
Hay una forma de hacer que fileb sea dinámico?