2002-12-27 08:53:07 +00:00
|
|
|
/* fat.c - FAT filesystem */
|
|
|
|
/*
|
2004-04-04 13:46:03 +00:00
|
|
|
* GRUB -- GRand Unified Bootloader
|
2004-03-14 17:48:25 +00:00
|
|
|
* Copyright (C) 2000,2001,2002,2003,2004 Free Software Foundation, Inc.
|
2002-12-27 08:53:07 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
#include <grub/fs.h>
|
|
|
|
#include <grub/disk.h>
|
|
|
|
#include <grub/file.h>
|
|
|
|
#include <grub/types.h>
|
|
|
|
#include <grub/misc.h>
|
|
|
|
#include <grub/mm.h>
|
|
|
|
#include <grub/err.h>
|
|
|
|
#include <grub/dl.h>
|
|
|
|
|
|
|
|
#define GRUB_FAT_DIR_ENTRY_SIZE 32
|
|
|
|
|
|
|
|
#define GRUB_FAT_ATTR_READ_ONLY 0x01
|
|
|
|
#define GRUB_FAT_ATTR_HIDDEN 0x02
|
|
|
|
#define GRUB_FAT_ATTR_SYSTEM 0x04
|
|
|
|
#define GRUB_FAT_ATTR_VOLUME_ID 0x08
|
|
|
|
#define GRUB_FAT_ATTR_DIRECTORY 0x10
|
|
|
|
#define GRUB_FAT_ATTR_ARCHIVE 0x20
|
|
|
|
|
|
|
|
#define GRUB_FAT_ATTR_LONG_NAME (GRUB_FAT_ATTR_READ_ONLY \
|
|
|
|
| GRUB_FAT_ATTR_HIDDEN \
|
|
|
|
| GRUB_FAT_ATTR_SYSTEM \
|
|
|
|
| GRUB_FAT_ATTR_VOLUME_ID)
|
|
|
|
#define GRUB_FAT_ATTR_VALID (GRUB_FAT_ATTR_READ_ONLY \
|
|
|
|
| GRUB_FAT_ATTR_HIDDEN \
|
|
|
|
| GRUB_FAT_ATTR_SYSTEM \
|
|
|
|
| GRUB_FAT_ATTR_DIRECTORY \
|
|
|
|
| GRUB_FAT_ATTR_ARCHIVE)
|
|
|
|
|
|
|
|
struct grub_fat_bpb
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_uint8_t jmp_boot[3];
|
|
|
|
grub_uint8_t oem_name[8];
|
|
|
|
grub_uint16_t bytes_per_sector;
|
|
|
|
grub_uint8_t sectors_per_cluster;
|
|
|
|
grub_uint16_t num_reserved_sectors;
|
|
|
|
grub_uint8_t num_fats;
|
|
|
|
grub_uint16_t num_root_entries;
|
|
|
|
grub_uint16_t num_total_sectors_16;
|
|
|
|
grub_uint8_t media;
|
|
|
|
grub_uint16_t sectors_per_fat_16;
|
|
|
|
grub_uint16_t sectors_per_track;
|
|
|
|
grub_uint16_t num_heads;
|
|
|
|
grub_uint32_t num_hidden_sectors;
|
|
|
|
grub_uint32_t num_total_sectors_32;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
/* The following fields are only used by FAT32. */
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_uint32_t sectors_per_fat_32;
|
|
|
|
grub_uint16_t extended_flags;
|
|
|
|
grub_uint16_t fs_version;
|
|
|
|
grub_uint32_t root_cluster;
|
|
|
|
grub_uint16_t fs_info;
|
|
|
|
grub_uint16_t backup_boot_sector;
|
2002-12-27 08:53:07 +00:00
|
|
|
} __attribute__ ((packed));
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
struct grub_fat_dir_entry
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_uint8_t name[11];
|
|
|
|
grub_uint8_t attr;
|
|
|
|
grub_uint8_t nt_reserved;
|
|
|
|
grub_uint8_t c_time_tenth;
|
|
|
|
grub_uint16_t c_time;
|
|
|
|
grub_uint16_t c_date;
|
|
|
|
grub_uint16_t a_date;
|
|
|
|
grub_uint16_t first_cluster_high;
|
|
|
|
grub_uint16_t w_time;
|
|
|
|
grub_uint16_t w_date;
|
|
|
|
grub_uint16_t first_cluster_low;
|
|
|
|
grub_uint32_t file_size;
|
2002-12-27 08:53:07 +00:00
|
|
|
} __attribute__ ((packed));
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
struct grub_fat_long_name_entry
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_uint8_t id;
|
|
|
|
grub_uint16_t name1[5];
|
|
|
|
grub_uint8_t attr;
|
|
|
|
grub_uint8_t reserved;
|
|
|
|
grub_uint8_t checksum;
|
|
|
|
grub_uint16_t name2[6];
|
|
|
|
grub_uint16_t first_cluster;
|
|
|
|
grub_uint16_t name3[2];
|
2002-12-27 08:53:07 +00:00
|
|
|
} __attribute__ ((packed));
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
struct grub_fat_data
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
|
|
|
int logical_sector_bits;
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_uint32_t num_sectors;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_uint16_t fat_sector;
|
|
|
|
grub_uint32_t sectors_per_fat;
|
2002-12-27 08:53:07 +00:00
|
|
|
int fat_size;
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_uint32_t root_cluster;
|
|
|
|
grub_uint32_t root_sector;
|
|
|
|
grub_uint32_t num_root_sectors;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
int cluster_bits;
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_uint32_t cluster_eof_mark;
|
|
|
|
grub_uint32_t cluster_sector;
|
|
|
|
grub_uint32_t num_clusters;
|
|
|
|
|
|
|
|
grub_uint8_t attr;
|
|
|
|
grub_ssize_t file_size;
|
|
|
|
grub_uint32_t file_cluster;
|
|
|
|
grub_uint32_t cur_cluster_num;
|
|
|
|
grub_uint32_t cur_cluster;
|
2002-12-27 08:53:07 +00:00
|
|
|
};
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
#ifndef GRUB_UTIL
|
|
|
|
static grub_dl_t my_mod;
|
2003-01-06 00:01:35 +00:00
|
|
|
#endif
|
|
|
|
|
2002-12-27 08:53:07 +00:00
|
|
|
static int
|
2004-03-14 17:48:25 +00:00
|
|
|
fat_log2 (unsigned x)
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (x == 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (i = 0; (x & 1) == 0; i++)
|
|
|
|
x >>= 1;
|
|
|
|
|
|
|
|
if (x != 1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
static struct grub_fat_data *
|
|
|
|
grub_fat_mount (grub_disk_t disk)
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
struct grub_fat_bpb bpb;
|
|
|
|
struct grub_fat_data *data = 0;
|
|
|
|
grub_uint32_t first_fat, magic;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
if (! disk)
|
|
|
|
goto fail;
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
data = (struct grub_fat_data *) grub_malloc (sizeof (*data));
|
2002-12-27 08:53:07 +00:00
|
|
|
if (! data)
|
|
|
|
goto fail;
|
2003-01-02 23:46:21 +00:00
|
|
|
|
2002-12-27 08:53:07 +00:00
|
|
|
/* Read the BPB. */
|
2004-04-04 13:46:03 +00:00
|
|
|
if (grub_disk_read (disk, 0, 0, sizeof (bpb), (char *) &bpb))
|
2002-12-27 08:53:07 +00:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* Get the sizes of logical sectors and clusters. */
|
2004-03-14 17:48:25 +00:00
|
|
|
data->logical_sector_bits =
|
2004-04-04 13:46:03 +00:00
|
|
|
fat_log2 (grub_le_to_cpu16 (bpb.bytes_per_sector));
|
|
|
|
if (data->logical_sector_bits < GRUB_DISK_SECTOR_BITS)
|
2002-12-27 08:53:07 +00:00
|
|
|
goto fail;
|
2004-04-04 13:46:03 +00:00
|
|
|
data->logical_sector_bits -= GRUB_DISK_SECTOR_BITS;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
2004-03-14 17:48:25 +00:00
|
|
|
data->cluster_bits = fat_log2 (bpb.sectors_per_cluster);
|
2002-12-27 08:53:07 +00:00
|
|
|
if (data->cluster_bits < 0)
|
|
|
|
goto fail;
|
|
|
|
data->cluster_bits += data->logical_sector_bits;
|
|
|
|
|
|
|
|
/* Get information about FATs. */
|
2004-04-04 13:46:03 +00:00
|
|
|
data->fat_sector = (grub_le_to_cpu16 (bpb.num_reserved_sectors)
|
2002-12-27 08:53:07 +00:00
|
|
|
<< data->logical_sector_bits);
|
|
|
|
if (data->fat_sector == 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
data->sectors_per_fat = ((bpb.sectors_per_fat_16
|
2004-04-04 13:46:03 +00:00
|
|
|
? grub_le_to_cpu16 (bpb.sectors_per_fat_16)
|
|
|
|
: grub_le_to_cpu32 (bpb.sectors_per_fat_32))
|
2002-12-27 08:53:07 +00:00
|
|
|
<< data->logical_sector_bits);
|
|
|
|
if (data->sectors_per_fat == 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* Get the number of sectors in this volume. */
|
|
|
|
data->num_sectors = ((bpb.num_total_sectors_16
|
2004-04-04 13:46:03 +00:00
|
|
|
? grub_le_to_cpu16 (bpb.num_total_sectors_16)
|
|
|
|
: grub_le_to_cpu32 (bpb.num_total_sectors_32))
|
2002-12-27 08:53:07 +00:00
|
|
|
<< data->logical_sector_bits);
|
|
|
|
if (data->num_sectors == 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* Get information about the root directory. */
|
|
|
|
if (bpb.num_fats == 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
data->root_sector = data->fat_sector + bpb.num_fats * data->sectors_per_fat;
|
|
|
|
data->num_root_sectors
|
2004-04-04 13:46:03 +00:00
|
|
|
= ((((grub_uint32_t) grub_le_to_cpu16 (bpb.num_root_entries)
|
|
|
|
* GRUB_FAT_DIR_ENTRY_SIZE
|
|
|
|
+ grub_le_to_cpu16 (bpb.bytes_per_sector) - 1)
|
|
|
|
>> (data->logical_sector_bits + GRUB_DISK_SECTOR_BITS))
|
2002-12-27 08:53:07 +00:00
|
|
|
<< (data->logical_sector_bits));
|
|
|
|
|
|
|
|
data->cluster_sector = data->root_sector + data->num_root_sectors;
|
|
|
|
data->num_clusters = (((data->num_sectors - data->cluster_sector)
|
|
|
|
>> (data->cluster_bits + data->logical_sector_bits))
|
|
|
|
+ 2);
|
|
|
|
|
|
|
|
if (data->num_clusters <= 2)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (! bpb.sectors_per_fat_16)
|
|
|
|
{
|
|
|
|
/* FAT32. */
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_uint16_t flags = grub_le_to_cpu16 (bpb.extended_flags);
|
2002-12-27 08:53:07 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
data->root_cluster = grub_le_to_cpu32 (bpb.root_cluster);
|
2002-12-27 08:53:07 +00:00
|
|
|
data->fat_size = 32;
|
|
|
|
data->cluster_eof_mark = 0x0ffffff8;
|
|
|
|
|
|
|
|
if (flags & 0x80)
|
|
|
|
{
|
|
|
|
/* Get an active FAT. */
|
|
|
|
unsigned active_fat = flags & 0xf;
|
|
|
|
|
|
|
|
if (active_fat > bpb.num_fats)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
data->fat_sector += active_fat * data->sectors_per_fat;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bpb.num_root_entries != 0 || bpb.fs_version != 0)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* FAT12 or FAT16. */
|
|
|
|
data->root_cluster = ~0UL;
|
|
|
|
|
|
|
|
if (data->num_clusters <= 4085 + 2)
|
|
|
|
{
|
|
|
|
/* FAT12. */
|
|
|
|
data->fat_size = 12;
|
|
|
|
data->cluster_eof_mark = 0x0ff8;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* FAT16. */
|
|
|
|
data->fat_size = 16;
|
|
|
|
data->cluster_eof_mark = 0xfff8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* More sanity checks. */
|
|
|
|
if (data->num_sectors <= data->fat_sector)
|
|
|
|
goto fail;
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
if (grub_disk_read (disk,
|
2002-12-27 08:53:07 +00:00
|
|
|
data->fat_sector,
|
|
|
|
0,
|
|
|
|
sizeof (first_fat),
|
|
|
|
(char *) &first_fat))
|
|
|
|
goto fail;
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
first_fat = grub_le_to_cpu32 (first_fat);
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
if (data->fat_size == 32)
|
|
|
|
{
|
|
|
|
first_fat &= 0x0fffffff;
|
|
|
|
magic = 0x0fffff00;
|
|
|
|
}
|
|
|
|
else if (data->fat_size == 16)
|
|
|
|
{
|
|
|
|
first_fat &= 0x0000ffff;
|
|
|
|
magic = 0xff00;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
first_fat &= 0x00000fff;
|
|
|
|
magic = 0x0f00;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (first_fat != (magic | bpb.media))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* Start from the root directory. */
|
|
|
|
data->file_cluster = data->root_cluster;
|
|
|
|
data->cur_cluster_num = ~0UL;
|
2004-04-04 13:46:03 +00:00
|
|
|
data->attr = GRUB_FAT_ATTR_DIRECTORY;
|
2002-12-27 08:53:07 +00:00
|
|
|
return data;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_free (data);
|
|
|
|
grub_error (GRUB_ERR_BAD_FS, "not a fat filesystem");
|
2002-12-27 08:53:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
static grub_ssize_t
|
|
|
|
grub_fat_read_data (grub_disk_t disk, struct grub_fat_data *data,
|
2002-12-27 08:53:07 +00:00
|
|
|
void (*read_hook) (unsigned long sector,
|
|
|
|
unsigned offset, unsigned length),
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_ssize_t offset, grub_ssize_t len, char *buf)
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_ssize_t size;
|
|
|
|
grub_uint32_t logical_cluster;
|
2002-12-27 08:53:07 +00:00
|
|
|
unsigned logical_cluster_bits;
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_ssize_t ret = 0;
|
2002-12-27 08:53:07 +00:00
|
|
|
unsigned long sector;
|
|
|
|
|
|
|
|
/* This is a special case. FAT12 and FAT16 doesn't have the root directory
|
|
|
|
in clusters. */
|
|
|
|
if (data->file_cluster == ~0UL)
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
size = (data->num_root_sectors << GRUB_DISK_SECTOR_BITS) - offset;
|
2002-12-27 08:53:07 +00:00
|
|
|
if (size > len)
|
|
|
|
size = len;
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
if (grub_disk_read (disk, data->root_sector, offset, size, buf))
|
2002-12-27 08:53:07 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate the logical cluster number and offset. */
|
|
|
|
logical_cluster_bits = (data->cluster_bits
|
|
|
|
+ data->logical_sector_bits
|
2004-04-04 13:46:03 +00:00
|
|
|
+ GRUB_DISK_SECTOR_BITS);
|
2002-12-27 08:53:07 +00:00
|
|
|
logical_cluster = offset >> logical_cluster_bits;
|
|
|
|
offset &= (1 << logical_cluster_bits) - 1;
|
|
|
|
|
|
|
|
if (logical_cluster < data->cur_cluster_num)
|
|
|
|
{
|
|
|
|
data->cur_cluster_num = 0;
|
|
|
|
data->cur_cluster = data->file_cluster;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (len)
|
|
|
|
{
|
|
|
|
while (logical_cluster > data->cur_cluster_num)
|
|
|
|
{
|
|
|
|
/* Find next cluster. */
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_uint32_t next_cluster;
|
2002-12-27 08:53:07 +00:00
|
|
|
unsigned long fat_offset;
|
|
|
|
|
|
|
|
switch (data->fat_size)
|
|
|
|
{
|
|
|
|
case 32:
|
|
|
|
fat_offset = data->cur_cluster << 2;
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
fat_offset = data->cur_cluster << 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* case 12: */
|
|
|
|
fat_offset = data->cur_cluster + (data->cur_cluster >> 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the FAT. */
|
2004-04-04 13:46:03 +00:00
|
|
|
if (grub_disk_read (disk, data->fat_sector, fat_offset,
|
2002-12-27 08:53:07 +00:00
|
|
|
(data->fat_size + 7) >> 3,
|
|
|
|
(char *) &next_cluster))
|
|
|
|
return -1;
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
next_cluster = grub_le_to_cpu32 (next_cluster);
|
2002-12-27 08:53:07 +00:00
|
|
|
switch (data->fat_size)
|
|
|
|
{
|
|
|
|
case 16:
|
|
|
|
next_cluster &= 0xFFFF;
|
|
|
|
break;
|
|
|
|
case 12:
|
|
|
|
if (data->cur_cluster & 1)
|
2003-01-02 23:46:21 +00:00
|
|
|
next_cluster >>= 4;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
next_cluster &= 0x0FFF;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-01-02 23:46:21 +00:00
|
|
|
#if 0
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_printf ("%s:%d: fat_size=%d, next_cluster=%u\n",
|
2003-01-02 23:46:21 +00:00
|
|
|
__FILE__, __LINE__, data->fat_size, next_cluster);
|
|
|
|
#endif
|
|
|
|
|
2002-12-27 08:53:07 +00:00
|
|
|
/* Check the end. */
|
|
|
|
if (next_cluster >= data->cluster_eof_mark)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (next_cluster < 2 || next_cluster >= data->num_clusters)
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_FS, "invalid cluster %u",
|
2003-01-02 23:46:21 +00:00
|
|
|
next_cluster);
|
2002-12-27 08:53:07 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->cur_cluster = next_cluster;
|
|
|
|
data->cur_cluster_num++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the data here. */
|
|
|
|
sector = (data->cluster_sector
|
|
|
|
+ ((data->cur_cluster - 2)
|
|
|
|
<< (data->cluster_bits + data->logical_sector_bits)));
|
|
|
|
size = (1 << logical_cluster_bits) - offset;
|
|
|
|
if (size > len)
|
|
|
|
size = len;
|
|
|
|
|
|
|
|
disk->read_hook = read_hook;
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_disk_read (disk, sector, offset, size, buf);
|
2002-12-27 08:53:07 +00:00
|
|
|
disk->read_hook = 0;
|
2004-04-04 13:46:03 +00:00
|
|
|
if (grub_errno)
|
2002-12-27 08:53:07 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
len -= size;
|
|
|
|
buf += size;
|
|
|
|
ret += size;
|
|
|
|
logical_cluster++;
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the underlying directory or file in PATH and return the
|
|
|
|
next path. If there is no next path or an error occurs, return NULL.
|
|
|
|
If HOOK is specified, call it with each file name. */
|
|
|
|
static char *
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_fat_find_dir (grub_disk_t disk, struct grub_fat_data *data,
|
2002-12-27 08:53:07 +00:00
|
|
|
const char *path,
|
|
|
|
int (*hook) (const char *filename, int dir))
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
struct grub_fat_dir_entry dir;
|
2002-12-27 08:53:07 +00:00
|
|
|
char *dirname, *dirp;
|
|
|
|
char *filename, *filep = 0;
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_uint16_t *unibuf;
|
2002-12-27 08:53:07 +00:00
|
|
|
int slot = -1, slots = -1;
|
|
|
|
int checksum = -1;
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_ssize_t offset = -sizeof(dir);
|
2002-12-27 08:53:07 +00:00
|
|
|
int call_hook;
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
if (! (data->attr & GRUB_FAT_ATTR_DIRECTORY))
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory");
|
2002-12-27 08:53:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extract a directory name. */
|
|
|
|
while (*path == '/')
|
|
|
|
path++;
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
dirp = grub_strchr (path, '/');
|
2002-12-27 08:53:07 +00:00
|
|
|
if (dirp)
|
|
|
|
{
|
|
|
|
unsigned len = dirp - path;
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
dirname = grub_malloc (len + 1);
|
2002-12-27 08:53:07 +00:00
|
|
|
if (! dirname)
|
|
|
|
return 0;
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_memcpy (dirname, path, len);
|
2002-12-27 08:53:07 +00:00
|
|
|
dirname[len] = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* This is actually a file. */
|
2004-04-04 13:46:03 +00:00
|
|
|
dirname = grub_strdup (path);
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
call_hook = (! dirp && hook);
|
|
|
|
|
|
|
|
/* Allocate space enough to hold a long name. */
|
2004-04-04 13:46:03 +00:00
|
|
|
filename = grub_malloc (0x40 * 13 * 4 + 1);
|
|
|
|
unibuf = (grub_uint16_t *) grub_malloc (0x40 * 13 * 2);
|
2002-12-27 08:53:07 +00:00
|
|
|
if (! filename || ! unibuf)
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_free (filename);
|
|
|
|
grub_free (unibuf);
|
|
|
|
grub_free (dirname);
|
2002-12-27 08:53:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
/* Adjust the offset. */
|
|
|
|
offset += sizeof (dir);
|
|
|
|
|
|
|
|
/* Read a directory entry. */
|
2004-04-04 13:46:03 +00:00
|
|
|
if ((grub_fat_read_data (disk, data, 0,
|
2002-12-27 08:53:07 +00:00
|
|
|
offset, sizeof (dir), (char *) &dir)
|
|
|
|
!= sizeof (dir))
|
|
|
|
|| dir.name[0] == 0)
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
if (grub_errno == GRUB_ERR_NONE && ! call_hook)
|
|
|
|
grub_error (GRUB_ERR_FILE_NOT_FOUND, "file not found");
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle long name entries. */
|
2004-04-04 13:46:03 +00:00
|
|
|
if (dir.attr == GRUB_FAT_ATTR_LONG_NAME)
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
struct grub_fat_long_name_entry *long_name
|
|
|
|
= (struct grub_fat_long_name_entry *) &dir;
|
|
|
|
grub_uint8_t id = long_name->id;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
if (id & 0x40)
|
|
|
|
{
|
|
|
|
id &= 0x3f;
|
|
|
|
slots = slot = id;
|
|
|
|
checksum = long_name->checksum;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id != slot || slot == 0 || checksum != long_name->checksum)
|
|
|
|
{
|
|
|
|
checksum = -1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
slot--;
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_memcpy (unibuf + slot * 13, long_name->name1, 5 * 2);
|
|
|
|
grub_memcpy (unibuf + slot * 13 + 5, long_name->name2, 6 * 2);
|
|
|
|
grub_memcpy (unibuf + slot * 13 + 11, long_name->name3, 2 * 2);
|
2002-12-27 08:53:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if this entry is valid. */
|
2004-04-04 13:46:03 +00:00
|
|
|
if (dir.name[0] == 0xe5 || (dir.attr & ~GRUB_FAT_ATTR_VALID))
|
2002-12-27 08:53:07 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* This is a workaround for Japanese. */
|
|
|
|
if (dir.name[0] == 0x05)
|
|
|
|
dir.name[0] = 0xe5;
|
|
|
|
|
|
|
|
if (checksum != -1 && slot == 0)
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_uint8_t sum;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
for (sum = 0, i = 0; i < sizeof (dir.name); i++)
|
|
|
|
sum = ((sum >> 1) | (sum << 7)) + dir.name[i];
|
|
|
|
|
|
|
|
if (sum == checksum)
|
|
|
|
{
|
2004-08-28 13:14:29 +00:00
|
|
|
int u;
|
|
|
|
|
|
|
|
for (u = 0; u < slots * 13; u++)
|
|
|
|
unibuf[u] = grub_le_to_cpu16 (unibuf[u]);
|
|
|
|
|
|
|
|
*grub_utf16_to_utf8 (filename, unibuf, slots * 13) = '\0';
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
if (*dirname == '\0' && call_hook)
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
if (hook (filename, dir.attr & GRUB_FAT_ATTR_DIRECTORY))
|
2002-12-27 08:53:07 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
checksum = -1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
if (grub_strcmp (dirname, filename) == 0)
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
|
|
|
if (call_hook)
|
2004-04-04 13:46:03 +00:00
|
|
|
hook (filename, dir.attr & GRUB_FAT_ATTR_DIRECTORY);
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checksum = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert the 8.3 file name. */
|
|
|
|
filep = filename;
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
for (i = 0; i < 8 && dir.name[i] && ! grub_isspace (dir.name[i]); i++)
|
|
|
|
*filep++ = grub_tolower (dir.name[i]);
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
*filep = '.';
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
for (i = 8; i < 11 && dir.name[i] && ! grub_isspace (dir.name[i]); i++)
|
|
|
|
*++filep = grub_tolower (dir.name[i]);
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
if (*filep != '.')
|
|
|
|
filep++;
|
|
|
|
|
|
|
|
*filep = '\0';
|
|
|
|
|
|
|
|
if (*dirname == '\0' && call_hook)
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
if (hook (filename, dir.attr & GRUB_FAT_ATTR_DIRECTORY))
|
2002-12-27 08:53:07 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-04-04 13:46:03 +00:00
|
|
|
else if (grub_strcmp (dirname, filename) == 0)
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
|
|
|
if (call_hook)
|
2004-04-04 13:46:03 +00:00
|
|
|
hook (filename, dir.attr & GRUB_FAT_ATTR_DIRECTORY);
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_free (filename);
|
|
|
|
grub_free (dirname);
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
data->attr = dir.attr;
|
2004-04-04 13:46:03 +00:00
|
|
|
data->file_size = grub_le_to_cpu32 (dir.file_size);
|
|
|
|
data->file_cluster = ((grub_le_to_cpu16 (dir.first_cluster_high) << 16)
|
|
|
|
| grub_le_to_cpu16 (dir.first_cluster_low));
|
2002-12-27 08:53:07 +00:00
|
|
|
data->cur_cluster_num = ~0UL;
|
|
|
|
|
|
|
|
return dirp;
|
|
|
|
}
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
static grub_err_t
|
|
|
|
grub_fat_dir (grub_device_t device, const char *path,
|
2002-12-27 08:53:07 +00:00
|
|
|
int (*hook) (const char *filename, int dir))
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
struct grub_fat_data *data = 0;
|
|
|
|
grub_disk_t disk = device->disk;
|
2002-12-27 08:53:07 +00:00
|
|
|
char *p = (char *) path;
|
2003-01-06 00:01:35 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
#ifndef GRUB_UTIL
|
|
|
|
grub_dl_ref (my_mod);
|
2003-01-06 00:01:35 +00:00
|
|
|
#endif
|
2002-12-27 08:53:07 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
data = grub_fat_mount (disk);
|
2002-12-27 08:53:07 +00:00
|
|
|
if (! data)
|
2003-01-06 00:01:35 +00:00
|
|
|
goto fail;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
p = grub_fat_find_dir (disk, data, p, hook);
|
2002-12-27 08:53:07 +00:00
|
|
|
}
|
2004-04-04 13:46:03 +00:00
|
|
|
while (p && grub_errno == GRUB_ERR_NONE);
|
2002-12-27 08:53:07 +00:00
|
|
|
|
2003-01-06 00:01:35 +00:00
|
|
|
fail:
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_free (data);
|
2003-01-06 00:01:35 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
#ifndef GRUB_UTIL
|
|
|
|
grub_dl_unref (my_mod);
|
2003-01-06 00:01:35 +00:00
|
|
|
#endif
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
return grub_errno;
|
2002-12-27 08:53:07 +00:00
|
|
|
}
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
static grub_err_t
|
|
|
|
grub_fat_open (grub_file_t file, const char *name)
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
struct grub_fat_data *data = 0;
|
2002-12-27 08:53:07 +00:00
|
|
|
char *p = (char *) name;
|
2003-01-06 00:01:35 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
#ifndef GRUB_UTIL
|
|
|
|
grub_dl_ref (my_mod);
|
2003-01-06 00:01:35 +00:00
|
|
|
#endif
|
2002-12-27 08:53:07 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
data = grub_fat_mount (file->device->disk);
|
2002-12-27 08:53:07 +00:00
|
|
|
if (! data)
|
2003-01-06 00:01:35 +00:00
|
|
|
goto fail;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
p = grub_fat_find_dir (file->device->disk, data, p, 0);
|
|
|
|
if (grub_errno != GRUB_ERR_NONE)
|
2002-12-27 08:53:07 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
while (p);
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
if (data->attr & GRUB_FAT_ATTR_DIRECTORY)
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a file");
|
2002-12-27 08:53:07 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
file->data = data;
|
|
|
|
file->size = data->file_size;
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
fail:
|
2003-01-06 00:01:35 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_free (data);
|
2003-01-06 00:01:35 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
#ifndef GRUB_UTIL
|
|
|
|
grub_dl_unref (my_mod);
|
2003-01-06 00:01:35 +00:00
|
|
|
#endif
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
return grub_errno;
|
2002-12-27 08:53:07 +00:00
|
|
|
}
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
static grub_ssize_t
|
|
|
|
grub_fat_read (grub_file_t file, char *buf, grub_ssize_t len)
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
return grub_fat_read_data (file->device->disk, file->data, file->read_hook,
|
2002-12-27 08:53:07 +00:00
|
|
|
file->offset, len, buf);
|
|
|
|
}
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
static grub_err_t
|
|
|
|
grub_fat_close (grub_file_t file)
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_free (file->data);
|
2003-01-06 00:01:35 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
#ifndef GRUB_UTIL
|
|
|
|
grub_dl_unref (my_mod);
|
2003-01-06 00:01:35 +00:00
|
|
|
#endif
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
return grub_errno;
|
2002-12-27 08:53:07 +00:00
|
|
|
}
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
static grub_err_t
|
|
|
|
grub_fat_label (grub_device_t device, char **label)
|
2003-12-03 19:17:27 +00:00
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
struct grub_fat_data *data;
|
|
|
|
grub_disk_t disk = device->disk;
|
|
|
|
grub_ssize_t offset = -sizeof(struct grub_fat_dir_entry);
|
2003-12-03 19:17:27 +00:00
|
|
|
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
#ifndef GRUB_UTIL
|
|
|
|
grub_dl_ref (my_mod);
|
2003-12-03 19:17:27 +00:00
|
|
|
#endif
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
data = grub_fat_mount (disk);
|
2003-12-03 19:17:27 +00:00
|
|
|
if (! data)
|
|
|
|
goto fail;
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
if (! (data->attr & GRUB_FAT_ATTR_DIRECTORY))
|
2003-12-03 19:17:27 +00:00
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory");
|
2003-12-03 19:17:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
struct grub_fat_dir_entry dir;
|
2003-12-03 19:17:27 +00:00
|
|
|
|
|
|
|
/* Adjust the offset. */
|
|
|
|
offset += sizeof (dir);
|
|
|
|
|
|
|
|
/* Read a directory entry. */
|
2004-04-04 13:46:03 +00:00
|
|
|
if ((grub_fat_read_data (disk, data, 0,
|
2003-12-03 19:17:27 +00:00
|
|
|
offset, sizeof (dir), (char *) &dir)
|
|
|
|
!= sizeof (dir))
|
|
|
|
|| dir.name[0] == 0)
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
if (grub_errno != GRUB_ERR_NONE)
|
2003-12-03 19:17:27 +00:00
|
|
|
goto fail;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*label = 0;
|
2004-04-04 13:46:03 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2003-12-03 19:17:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
if (dir.attr == GRUB_FAT_ATTR_VOLUME_ID)
|
2003-12-03 19:17:27 +00:00
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
*label = grub_strndup (dir.name, 11);
|
|
|
|
return GRUB_ERR_NONE;
|
2003-12-03 19:17:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*label = 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
#ifndef GRUB_UTIL
|
|
|
|
grub_dl_unref (my_mod);
|
2003-12-03 19:17:27 +00:00
|
|
|
#endif
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_free (data);
|
2003-12-03 19:17:27 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
return grub_errno;
|
2003-12-03 19:17:27 +00:00
|
|
|
}
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
static struct grub_fs grub_fat_fs =
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
|
|
|
.name = "fat",
|
2004-04-04 13:46:03 +00:00
|
|
|
.dir = grub_fat_dir,
|
|
|
|
.open = grub_fat_open,
|
|
|
|
.read = grub_fat_read,
|
|
|
|
.close = grub_fat_close,
|
|
|
|
.label = grub_fat_label,
|
2002-12-27 08:53:07 +00:00
|
|
|
.next = 0
|
|
|
|
};
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
#ifdef GRUB_UTIL
|
2003-01-06 00:01:35 +00:00
|
|
|
void
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_fat_init (void)
|
2003-01-06 00:01:35 +00:00
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_fs_register (&grub_fat_fs);
|
2003-01-06 00:01:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_fat_fini (void)
|
2003-01-06 00:01:35 +00:00
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_fs_unregister (&grub_fat_fs);
|
2003-01-06 00:01:35 +00:00
|
|
|
}
|
2004-04-04 13:46:03 +00:00
|
|
|
#else /* ! GRUB_UTIL */
|
|
|
|
GRUB_MOD_INIT
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_fs_register (&grub_fat_fs);
|
2003-01-06 00:01:35 +00:00
|
|
|
my_mod = mod;
|
2002-12-27 08:53:07 +00:00
|
|
|
}
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
GRUB_MOD_FINI
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_fs_unregister (&grub_fat_fs);
|
2002-12-27 08:53:07 +00:00
|
|
|
}
|
2004-04-04 13:46:03 +00:00
|
|
|
#endif /* ! GRUB_UTIL */
|