vaseboot/VasEBoot-core/fs/ntfs.c

1672 lines
44 KiB
C

/* ntfs.c - NTFS filesystem */
/*
* VAS_EBOOT -- GRand Unified Bootloader
* Copyright (C) 2007,2008,2009 Free Software Foundation, Inc.
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#define VasEBoot_fshelp_node VasEBoot_ntfs_file
#include <VasEBoot/file.h>
#include <VasEBoot/mm.h>
#include <VasEBoot/misc.h>
#include <VasEBoot/disk.h>
#include <VasEBoot/dl.h>
#include <VasEBoot/fshelp.h>
#include <VasEBoot/ntfs.h>
#include <VasEBoot/charset.h>
#include <VasEBoot/lockdown.h>
VAS_EBOOT_MOD_LICENSE ("GPLv3+");
static VasEBoot_dl_t my_mod;
#define VasEBoot_fshelp_node VasEBoot_ntfs_file
static inline VasEBoot_uint16_t
u16at (void *ptr, VasEBoot_size_t ofs)
{
return VasEBoot_le_to_cpu16 (VasEBoot_get_unaligned16 ((char *) ptr + ofs));
}
static inline VasEBoot_uint32_t
u32at (void *ptr, VasEBoot_size_t ofs)
{
return VasEBoot_le_to_cpu32 (VasEBoot_get_unaligned32 ((char *) ptr + ofs));
}
static inline VasEBoot_uint64_t
u64at (void *ptr, VasEBoot_size_t ofs)
{
return VasEBoot_le_to_cpu64 (VasEBoot_get_unaligned64 ((char *) ptr + ofs));
}
static VasEBoot_uint16_t
first_attr_off (void *mft_buf_ptr)
{
return u16at (mft_buf_ptr, 0x14);
}
static VasEBoot_uint16_t
res_attr_data_off (void *res_attr_ptr)
{
return u16at (res_attr_ptr, 0x14);
}
static VasEBoot_uint32_t
res_attr_data_len (void *res_attr_ptr)
{
return u32at (res_attr_ptr, 0x10);
}
/*
* Check if the attribute is valid and doesn't exceed the allocated region.
* This accounts for resident and non-resident data.
*
* This is based off the documentation from the linux-ntfs project:
* https://flatcap.github.io/linux-ntfs/ntfs/concepts/attribute_header.html
*/
static bool
validate_attribute (VasEBoot_uint8_t *attr, void *end)
{
VasEBoot_size_t attr_size = 0;
VasEBoot_size_t min_size = 0;
VasEBoot_size_t run_size = 0;
VasEBoot_size_t spare = (VasEBoot_uint8_t *) end - attr;
/*
* Just used as a temporary variable to try and deal with cases where someone
* tries to overlap fields.
*/
VasEBoot_size_t curr = 0;
/* Need verify we can entirely read the attributes header. */
if (attr + VAS_EBOOT_NTFS_ATTRIBUTE_HEADER_SIZE >= (VasEBoot_uint8_t *) end)
goto fail;
/*
* So, the rest of this code uses a 16bit int for the attribute length but
* from reading the all the documentation I could find it says this field is
* actually 32bit. But let's be consistent with the rest of the code.
*
* https://elixir.bootlin.com/linux/v6.10.7/source/fs/ntfs3/ntfs.h#L370
*/
attr_size = u16at (attr, VAS_EBOOT_NTFS_ATTRIBUTE_LENGTH);
if (attr_size > spare)
goto fail;
/* Not an error case, just reached the end of the attributes. */
if (attr_size == 0)
return false;
/*
* Extra validation by trying to calculate a minimum possible size for this
* attribute. +8 from the size of the resident data struct which is the
* minimum that can be added.
*/
min_size = VAS_EBOOT_NTFS_ATTRIBUTE_HEADER_SIZE + 8;
if (min_size > attr_size)
goto fail;
/* Is the data is resident (0) or not (1). */
if (attr[VAS_EBOOT_NTFS_ATTRIBUTE_RESIDENT] == 0)
{
/* Read the offset and size of the attribute. */
curr = u16at (attr, VAS_EBOOT_NTFS_ATTRIBUTE_RES_OFFSET);
curr += u32at (attr, VAS_EBOOT_NTFS_ATTRIBUTE_RES_LENGTH);
if (curr > min_size)
min_size = curr;
}
else
{
/*
* If the data is non-resident, the minimum size is 64 which is where
* the data runs start. We already have a minimum size of 24. So, just
* adding 40 to get to the real value.
*/
min_size += 40;
if (min_size > attr_size)
goto fail;
/* If the compression unit size is > 0, +8 bytes*/
if (u16at (attr, VAS_EBOOT_NTFS_ATTRIBUTE_COMPRESSION_UNIT_SIZE) > 0)
min_size += 8;
/*
* Need to consider the data runs now. Each member of the run has byte
* that describes the size of the data length and offset. Each being
* 4 bits in the byte.
*/
curr = u16at (attr, VAS_EBOOT_NTFS_ATTRIBUTE_DATA_RUNS);
if (curr + 1 > min_size)
min_size = curr + 1;
if (min_size > attr_size)
goto fail;
/*
* Each attribute can store multiple data runs which are stored
* continuously in the attribute. They exist as one header byte
* with up to 14 bytes following it depending on the lengths.
* We stop when we hit a header that is just a NUL byte.
*
* https://flatcap.github.io/linux-ntfs/ntfs/concepts/data_runs.html
*/
while (attr[curr] != 0)
{
/*
* We stop when we hit a header that is just a NUL byte. The data
* run header is stored as a single byte where the top 4 bits refer
* to the number of bytes used to store the total length of the
* data run, and the number of bytes used to store the offset.
* These directly follow the header byte, so we use them to update
* the minimum size. Increment by one more than run size to move on
* to the next run size header byte. An example is a run size field
* value of 0x32, 3 + 2 = 5 bytes follow the run size. Increment
* by 5 to get to the end of this data run then one more to get to
* the start of the next run size byte.
*/
run_size = (attr[curr] & 0x7) + ((attr[curr] >> 4) & 0x7);
curr += (run_size + 1);
min_size += (run_size + 1);
if (min_size > attr_size)
goto fail;
}
}
/* Name offset, doing this after data residence checks. */
if (u16at (attr, VAS_EBOOT_NTFS_ATTRIBUTE_NAME_OFFSET) != 0)
{
curr = u16at (attr, VAS_EBOOT_NTFS_ATTRIBUTE_NAME_OFFSET);
/*
* Multiple the name length by 2 as its UTF-16. Can be zero if this in an
* unamed attribute.
*/
curr += attr[VAS_EBOOT_NTFS_ATTRIBUTE_NAME_LENGTH] * 2;
if (curr > min_size)
min_size = curr;
}
/* Padded to 8 bytes. */
if (min_size % 8 != 0)
min_size += 8 - (min_size % 8);
/*
* At this point min_size should be exactly attr_size but being flexible
* here to avoid any issues.
*/
if (min_size > attr_size)
goto fail;
return true;
fail:
VasEBoot_dprintf ("ntfs", "spare=%" PRIuVAS_EBOOT_SIZE " min_size=%" PRIuVAS_EBOOT_SIZE " attr_size=%" PRIuVAS_EBOOT_SIZE "\n",
spare, min_size, attr_size);
return false;
}
/* Return the next attribute if it exists, otherwise return NULL. */
static VasEBoot_uint8_t *
next_attribute (VasEBoot_uint8_t *curr_attribute, void *end, bool validate)
{
VasEBoot_uint8_t *next = curr_attribute;
/*
* Need to verify we aren't exceeding the end of the buffer by reading the
* header for the current attribute
*/
if (curr_attribute + VAS_EBOOT_NTFS_ATTRIBUTE_HEADER_SIZE >= (VasEBoot_uint8_t *) end)
return NULL;
next += u16at (curr_attribute, 4);
if (validate)
{
if (validate_attribute (next, end) == false)
return NULL;
}
else if (next >= (VasEBoot_uint8_t *) end)
return NULL;
return next;
}
VasEBoot_ntfscomp_func_t VasEBoot_ntfscomp_func;
static VasEBoot_err_t
fixup (VasEBoot_uint8_t *buf, VasEBoot_size_t len, const VasEBoot_uint8_t *magic)
{
VasEBoot_uint16_t ss;
VasEBoot_uint8_t *pu;
VasEBoot_uint16_t us;
VasEBoot_uint16_t pu_offset;
COMPILE_TIME_ASSERT ((1 << VAS_EBOOT_NTFS_BLK_SHR) == VAS_EBOOT_DISK_SECTOR_SIZE);
if (VasEBoot_memcmp (buf, magic, 4))
return VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "%s label not found", magic);
ss = u16at (buf, 6) - 1;
if (ss != len)
return VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "size not match");
pu_offset = u16at (buf, 4);
if (pu_offset >= (len * VAS_EBOOT_DISK_SECTOR_SIZE - (2 * ss)))
return VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "pu offset size incorrect");
pu = buf + pu_offset;
us = u16at (pu, 0);
buf -= 2;
while (ss > 0)
{
buf += VAS_EBOOT_DISK_SECTOR_SIZE;
pu += 2;
if (u16at (buf, 0) != us)
return VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "fixup signature not match");
buf[0] = pu[0];
buf[1] = pu[1];
ss--;
}
return 0;
}
static VasEBoot_err_t read_mft (struct VasEBoot_ntfs_data *data, VasEBoot_uint8_t *buf,
VasEBoot_uint64_t mftno);
static VasEBoot_err_t read_attr (struct VasEBoot_ntfs_attr *at, VasEBoot_uint8_t *dest,
VasEBoot_disk_addr_t ofs, VasEBoot_size_t len,
int cached,
VasEBoot_disk_read_hook_t read_hook,
void *read_hook_data);
static VasEBoot_err_t read_data (struct VasEBoot_ntfs_attr *at, VasEBoot_uint8_t *pa,
VasEBoot_uint8_t *dest,
VasEBoot_disk_addr_t ofs, VasEBoot_size_t len,
int cached,
VasEBoot_disk_read_hook_t read_hook,
void *read_hook_data);
static VasEBoot_err_t
init_attr (struct VasEBoot_ntfs_attr *at, struct VasEBoot_ntfs_file *mft)
{
at->mft = mft;
at->flags = (mft == &mft->data->mmft) ? VAS_EBOOT_NTFS_AF_MMFT : 0;
at->attr_nxt = mft->buf + first_attr_off (mft->buf);
at->end = mft->buf + (mft->data->mft_size << VAS_EBOOT_NTFS_BLK_SHR);
if (at->attr_nxt > at->end)
return VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "attributes start outside the MFT");
at->attr_end = at->emft_buf = at->edat_buf = at->sbuf = NULL;
return VAS_EBOOT_ERR_NONE;
}
static void
free_attr (struct VasEBoot_ntfs_attr *at)
{
VasEBoot_free (at->emft_buf);
VasEBoot_free (at->edat_buf);
VasEBoot_free (at->sbuf);
}
static VasEBoot_uint8_t *
find_attr (struct VasEBoot_ntfs_attr *at, VasEBoot_uint8_t attr)
{
VasEBoot_uint8_t *mft_end;
VasEBoot_uint16_t nsize;
VasEBoot_uint16_t nxt_offset;
VasEBoot_uint32_t edat_offset;
/* VAS_EBOOT_NTFS_AF_ALST indicates the attribute list type */
if (at->flags & VAS_EBOOT_NTFS_AF_ALST)
{
retry:
while (at->attr_nxt)
{
at->attr_cur = at->attr_nxt;
/*
* Go to the next attribute in the list but do not validate
* because this is the attribute list type.
*/
at->attr_nxt = next_attribute (at->attr_cur, at->attr_end, false);
if ((*at->attr_cur == attr) || (attr == 0))
{
VasEBoot_uint8_t *new_pos, *end;
if (at->flags & VAS_EBOOT_NTFS_AF_MMFT)
{
if ((VasEBoot_disk_read
(at->mft->data->disk, u32at (at->attr_cur, 0x10), 0,
512, at->emft_buf))
||
(VasEBoot_disk_read
(at->mft->data->disk, u32at (at->attr_cur, 0x14), 0,
512, at->emft_buf + 512)))
return NULL;
if (fixup (at->emft_buf, at->mft->data->mft_size,
(const VasEBoot_uint8_t *) "FILE"))
return NULL;
}
else
{
if (read_mft (at->mft->data, at->emft_buf,
u32at (at->attr_cur, 0x10)))
return NULL;
}
/*
* Only time emft_bufs is defined is in this function, with this
* size.
*/
VasEBoot_size_t emft_buf_size =
at->mft->data->mft_size << VAS_EBOOT_NTFS_BLK_SHR;
/*
* Needs to be enough space for the successful case to even
* bother.
*/
if (first_attr_off (at->emft_buf) >= (emft_buf_size - 0x18 - 2))
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_FS,
"can\'t find 0x%X in attribute list",
(unsigned char) *at->attr_cur);
return NULL;
}
new_pos = &at->emft_buf[first_attr_off (at->emft_buf)];
end = &at->emft_buf[emft_buf_size];
at->end = end;
while (new_pos && *new_pos != 0xFF)
{
if ((*new_pos == *at->attr_cur)
&& (u16at (new_pos, 0xE) == u16at (at->attr_cur, 0x18)))
{
return new_pos;
}
/*
* Go to the next attribute in the list but do not validate
* because this is the attribute list type.
*/
new_pos = next_attribute (new_pos, end, false);
}
VasEBoot_error (VAS_EBOOT_ERR_BAD_FS,
"can\'t find 0x%X in attribute list",
(unsigned char) *at->attr_cur);
return NULL;
}
}
return NULL;
}
at->attr_cur = at->attr_nxt;
mft_end = at->mft->buf + (at->mft->data->mft_size << VAS_EBOOT_NTFS_BLK_SHR);
while (at->attr_cur >= at->mft->buf && at->attr_cur < (mft_end - 4)
&& *at->attr_cur != 0xFF)
{
/*
* We can't use validate_attribute here because this logic
* seems to be used for both parsing through attributes
* and attribute lists.
*/
nsize = u16at (at->attr_cur, 4);
if (at->attr_cur + VasEBoot_max (VAS_EBOOT_NTFS_ATTRIBUTE_HEADER_SIZE, nsize) >= at->end)
{
at->attr_nxt = at->attr_cur;
break;
}
else
at->attr_nxt = at->attr_cur + nsize;
if (*at->attr_cur == VAS_EBOOT_NTFS_AT_ATTRIBUTE_LIST)
at->attr_end = at->attr_cur;
if ((*at->attr_cur == attr) || (attr == 0) || (nsize == 0))
return at->attr_cur;
at->attr_cur = at->attr_nxt;
}
if (at->attr_end)
{
VasEBoot_uint8_t *pa, *pa_end;
at->emft_buf = VasEBoot_malloc (at->mft->data->mft_size << VAS_EBOOT_NTFS_BLK_SHR);
if (at->emft_buf == NULL)
return NULL;
pa = at->attr_end;
if (pa[8])
{
VasEBoot_uint32_t n;
n = ((u32at (pa, 0x30) + VAS_EBOOT_DISK_SECTOR_SIZE - 1)
& (~(VAS_EBOOT_DISK_SECTOR_SIZE - 1)));
at->attr_cur = at->attr_end;
at->edat_buf = VasEBoot_malloc (n);
if (!at->edat_buf)
return NULL;
if (read_data (at, pa, at->edat_buf, 0, n, 0, 0, 0))
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_FS,
"fail to read non-resident attribute list");
return NULL;
}
at->attr_nxt = at->edat_buf;
edat_offset = u32at (pa, 0x30);
if (edat_offset >= n)
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "edat offset is out of bounds");
return NULL;
}
at->attr_end = at->edat_buf + edat_offset;
pa_end = at->edat_buf + n;
}
else
{
at->attr_nxt = at->attr_end + res_attr_data_off (pa);
edat_offset = u32at (pa, 4);
if ((at->attr_end + edat_offset) >= (at->end))
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "edat offset is out of bounds");
return NULL;
}
at->attr_end = at->attr_end + edat_offset;
pa_end = at->mft->buf + (at->mft->data->mft_size << VAS_EBOOT_NTFS_BLK_SHR);
}
at->flags |= VAS_EBOOT_NTFS_AF_ALST;
/* From this point on pa_end is the end of the buffer */
at->end = pa_end;
if (at->attr_end >= pa_end || at->attr_nxt >= pa_end)
return NULL;
while (at->attr_nxt)
{
if ((*at->attr_nxt == attr) || (attr == 0))
break;
nxt_offset = u16at (at->attr_nxt, 4);
at->attr_nxt += nxt_offset;
/*
* Stop and set attr_nxt to NULL when either the next offset is zero,
* or when the pointer is within four bytes of the end of the buffer
* since we could attempt to access attr_nxt + 4 bytes offset above to
* get the next 16-bit 'nxt_offset' value.
*/
if (nxt_offset == 0 || at->attr_nxt >= (pa_end - 4))
at->attr_nxt = NULL;
}
if ((at->attr_nxt + VAS_EBOOT_NTFS_ATTRIBUTE_HEADER_SIZE) >= at->attr_end || at->attr_nxt == NULL)
return NULL;
if ((at->flags & VAS_EBOOT_NTFS_AF_MMFT) && (attr == VAS_EBOOT_NTFS_AT_DATA))
{
at->flags |= VAS_EBOOT_NTFS_AF_GPOS;
at->attr_cur = at->attr_nxt;
pa = at->attr_cur;
if ((pa >= pa_end) || (pa_end - pa < 0x18))
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "can\'t parse attribute list");
return NULL;
}
VasEBoot_set_unaligned32 ((char *) pa + 0x10,
VasEBoot_cpu_to_le32 (at->mft->data->mft_start));
VasEBoot_set_unaligned32 ((char *) pa + 0x14,
VasEBoot_cpu_to_le32 (at->mft->data->mft_start
+ 1));
pa = at->attr_nxt + u16at (pa, 4);
if (pa >= pa_end)
pa = NULL;
while (pa)
{
if (*pa != attr)
break;
if ((pa >= pa_end) || (pa_end - pa < 0x18))
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "can\'t parse attribute list");
return NULL;
}
if (read_attr
(at, pa + 0x10,
u32at (pa, 0x10) * (at->mft->data->mft_size << VAS_EBOOT_NTFS_BLK_SHR),
at->mft->data->mft_size << VAS_EBOOT_NTFS_BLK_SHR, 0, 0, 0))
return NULL;
pa += u16at (pa, 4);
if (pa >= pa_end)
pa = NULL;
}
at->attr_nxt = at->attr_cur;
at->flags &= ~VAS_EBOOT_NTFS_AF_GPOS;
}
goto retry;
}
return NULL;
}
static VasEBoot_uint8_t *
locate_attr (struct VasEBoot_ntfs_attr *at, struct VasEBoot_ntfs_file *mft,
VasEBoot_uint8_t attr)
{
VasEBoot_uint8_t *pa;
VasEBoot_uint8_t *last_pa;
if (init_attr (at, mft) != VAS_EBOOT_ERR_NONE)
return NULL;
pa = find_attr (at, attr);
if (pa == NULL)
return NULL;
if ((at->flags & VAS_EBOOT_NTFS_AF_ALST) == 0)
{
/* Used to make sure we're not stuck in a loop. */
last_pa = NULL;
while (1)
{
pa = find_attr (at, attr);
if (pa == NULL || pa == last_pa)
break;
if (at->flags & VAS_EBOOT_NTFS_AF_ALST)
return pa;
last_pa = pa;
}
VasEBoot_errno = VAS_EBOOT_ERR_NONE;
free_attr (at);
if (init_attr (at, mft) != VAS_EBOOT_ERR_NONE)
return NULL;
pa = find_attr (at, attr);
}
return pa;
}
static VasEBoot_disk_addr_t
read_run_data (const VasEBoot_uint8_t *run, int nn, int sig)
{
VasEBoot_uint64_t r = 0;
if (sig && nn && (run[nn - 1] & 0x80))
r = -1;
VasEBoot_memcpy (&r, run, nn);
return VasEBoot_le_to_cpu64 (r);
}
VasEBoot_err_t
VasEBoot_ntfs_read_run_list (struct VasEBoot_ntfs_rlst * ctx)
{
VasEBoot_uint8_t c1, c2;
VasEBoot_disk_addr_t val;
VasEBoot_uint8_t *run;
run = ctx->cur_run;
retry:
c1 = ((*run) & 0x7);
c2 = ((*run) >> 4) & 0x7;
run++;
if (!c1)
{
if ((ctx->attr) && (ctx->attr->flags & VAS_EBOOT_NTFS_AF_ALST))
{
VasEBoot_disk_read_hook_t save_hook;
save_hook = ctx->comp.disk->read_hook;
ctx->comp.disk->read_hook = 0;
run = find_attr (ctx->attr, *ctx->attr->attr_cur);
ctx->comp.disk->read_hook = save_hook;
if (run)
{
if (run[8] == 0)
return VasEBoot_error (VAS_EBOOT_ERR_BAD_FS,
"$DATA should be non-resident");
run += u16at (run, 0x20);
ctx->curr_lcn = 0;
goto retry;
}
}
return VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "run list overflow");
}
ctx->curr_vcn = ctx->next_vcn;
ctx->next_vcn += read_run_data (run, c1, 0); /* length of current VCN */
run += c1;
val = read_run_data (run, c2, 1); /* offset to previous LCN */
run += c2;
ctx->curr_lcn += val;
if (val == 0)
ctx->flags |= VAS_EBOOT_NTFS_RF_BLNK;
else
ctx->flags &= ~VAS_EBOOT_NTFS_RF_BLNK;
ctx->cur_run = run;
return 0;
}
static VasEBoot_disk_addr_t
VasEBoot_ntfs_read_block (VasEBoot_fshelp_node_t node, VasEBoot_disk_addr_t block)
{
struct VasEBoot_ntfs_rlst *ctx;
ctx = (struct VasEBoot_ntfs_rlst *) node;
if (block >= ctx->next_vcn)
{
if (VasEBoot_ntfs_read_run_list (ctx))
return -1;
return ctx->curr_lcn;
}
else
return (ctx->flags & VAS_EBOOT_NTFS_RF_BLNK) ? 0 : (block -
ctx->curr_vcn + ctx->curr_lcn);
}
static VasEBoot_err_t
read_data (struct VasEBoot_ntfs_attr *at, VasEBoot_uint8_t *pa, VasEBoot_uint8_t *dest,
VasEBoot_disk_addr_t ofs, VasEBoot_size_t len, int cached,
VasEBoot_disk_read_hook_t read_hook, void *read_hook_data)
{
struct VasEBoot_ntfs_rlst cc, *ctx;
VasEBoot_uint8_t *end_ptr = (pa + len);
VasEBoot_uint16_t run_offset;
if (len == 0)
return 0;
VasEBoot_memset (&cc, 0, sizeof (cc));
ctx = &cc;
ctx->attr = at;
ctx->comp.log_spc = at->mft->data->log_spc;
ctx->comp.disk = at->mft->data->disk;
if (read_hook == VasEBoot_file_progress_hook)
ctx->file = read_hook_data;
if (pa[8] == 0)
{
if (ofs + len > res_attr_data_len (pa))
return VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "read out of range");
if (res_attr_data_len (pa) > (at->mft->data->mft_size << VAS_EBOOT_NTFS_BLK_SHR))
return VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "resident attribute too large");
if (pa >= at->mft->buf + (at->mft->data->mft_size << VAS_EBOOT_NTFS_BLK_SHR))
return VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "resident attribute out of range");
if (res_attr_data_off (pa) + res_attr_data_len (pa) >
(VasEBoot_addr_t) at->mft->buf + (at->mft->data->mft_size << VAS_EBOOT_NTFS_BLK_SHR) - (VasEBoot_addr_t) pa)
return VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "resident attribute out of range");
VasEBoot_memcpy (dest, pa + res_attr_data_off (pa) + ofs, len);
return 0;
}
run_offset = u16at (pa, 0x20);
if ((run_offset + pa) >= end_ptr || ((run_offset + pa) >= (at->end)))
return VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "run offset out of range");
ctx->cur_run = pa + run_offset;
ctx->next_vcn = u32at (pa, 0x10);
ctx->curr_lcn = 0;
if ((pa[0xC] & VAS_EBOOT_NTFS_FLAG_COMPRESSED)
&& !(at->flags & VAS_EBOOT_NTFS_AF_GPOS))
{
if (!cached)
return VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "attribute can\'t be compressed");
return (VasEBoot_ntfscomp_func) ? VasEBoot_ntfscomp_func (dest, ofs, len, ctx)
: VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, N_("module `%s' isn't loaded"),
"ntfscomp");
}
ctx->target_vcn = ofs >> (VAS_EBOOT_NTFS_BLK_SHR + ctx->comp.log_spc);
while (ctx->next_vcn <= ctx->target_vcn)
{
if (VasEBoot_ntfs_read_run_list (ctx))
return VasEBoot_errno;
}
if (at->flags & VAS_EBOOT_NTFS_AF_GPOS)
{
VasEBoot_disk_addr_t st0, st1;
VasEBoot_uint64_t m;
m = (ofs >> VAS_EBOOT_NTFS_BLK_SHR) & ((1 << ctx->comp.log_spc) - 1);
st0 =
((ctx->target_vcn - ctx->curr_vcn + ctx->curr_lcn) << ctx->comp.log_spc) + m;
st1 = st0 + 1;
if (st1 ==
(ctx->next_vcn - ctx->curr_vcn + ctx->curr_lcn) << ctx->comp.log_spc)
{
if (VasEBoot_ntfs_read_run_list (ctx))
return VasEBoot_errno;
st1 = ctx->curr_lcn << ctx->comp.log_spc;
}
VasEBoot_set_unaligned32 (dest, VasEBoot_cpu_to_le32 (st0));
VasEBoot_set_unaligned32 (dest + 4, VasEBoot_cpu_to_le32 (st1));
return 0;
}
VasEBoot_fshelp_read_file (ctx->comp.disk, (VasEBoot_fshelp_node_t) ctx,
read_hook, read_hook_data, ofs, len,
(char *) dest,
VasEBoot_ntfs_read_block, ofs + len,
ctx->comp.log_spc, 0);
return VasEBoot_errno;
}
static VasEBoot_err_t
read_attr (struct VasEBoot_ntfs_attr *at, VasEBoot_uint8_t *dest, VasEBoot_disk_addr_t ofs,
VasEBoot_size_t len, int cached,
VasEBoot_disk_read_hook_t read_hook, void *read_hook_data)
{
VasEBoot_uint8_t *save_cur;
VasEBoot_uint8_t attr;
VasEBoot_uint8_t *pp;
VasEBoot_err_t ret;
if (at == NULL || at->attr_cur == NULL)
return VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "attribute not found");
save_cur = at->attr_cur;
at->attr_nxt = at->attr_cur;
attr = *at->attr_nxt;
if (at->flags & VAS_EBOOT_NTFS_AF_ALST)
{
VasEBoot_uint8_t *pa;
VasEBoot_disk_addr_t vcn;
/* If compression is possible make sure that we include possible
compressed block size. */
if (VAS_EBOOT_NTFS_LOG_COM_SEC >= at->mft->data->log_spc)
vcn = ((ofs >> VAS_EBOOT_NTFS_COM_LOG_LEN)
<< (VAS_EBOOT_NTFS_LOG_COM_SEC - at->mft->data->log_spc)) & ~0xFULL;
else
vcn = ofs >> (at->mft->data->log_spc + VAS_EBOOT_NTFS_BLK_SHR);
pa = at->attr_nxt + u16at (at->attr_nxt, 4);
if (validate_attribute (pa, at->attr_end) == false)
pa = NULL;
while (pa)
{
if (*pa != attr)
break;
if (u32at (pa, 8) > vcn)
break;
at->attr_nxt = pa;
pa = next_attribute (pa, at->attr_end, true);
}
}
pp = find_attr (at, attr);
if (pp)
ret = read_data (at, pp, dest, ofs, len, cached,
read_hook, read_hook_data);
else
ret =
(VasEBoot_errno) ? VasEBoot_errno : VasEBoot_error (VAS_EBOOT_ERR_BAD_FS,
"attribute not found");
at->attr_cur = save_cur;
return ret;
}
static VasEBoot_err_t
read_mft (struct VasEBoot_ntfs_data *data, VasEBoot_uint8_t *buf, VasEBoot_uint64_t mftno)
{
if (read_attr
(&data->mmft.attr, buf, mftno * ((VasEBoot_disk_addr_t) data->mft_size << VAS_EBOOT_NTFS_BLK_SHR),
data->mft_size << VAS_EBOOT_NTFS_BLK_SHR, 0, 0, 0))
return VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "read MFT 0x%llx fails", (unsigned long long) mftno);
return fixup (buf, data->mft_size, (const VasEBoot_uint8_t *) "FILE");
}
static VasEBoot_err_t
init_file (struct VasEBoot_ntfs_file *mft, VasEBoot_uint64_t mftno)
{
unsigned short flag;
mft->inode_read = 1;
mft->buf = VasEBoot_malloc (mft->data->mft_size << VAS_EBOOT_NTFS_BLK_SHR);
if (mft->buf == NULL)
return VasEBoot_errno;
if (read_mft (mft->data, mft->buf, mftno))
return VasEBoot_errno;
flag = u16at (mft->buf, 0x16);
if ((flag & 1) == 0)
return VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "MFT 0x%llx is not in use",
(unsigned long long) mftno);
if ((flag & 2) == 0)
{
VasEBoot_uint8_t *pa;
pa = locate_attr (&mft->attr, mft, VAS_EBOOT_NTFS_AT_DATA);
if (pa == NULL)
return VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "no $DATA in MFT 0x%llx",
(unsigned long long) mftno);
if (!pa[8])
mft->size = res_attr_data_len (pa);
else
mft->size = u64at (pa, 0x30);
if ((mft->attr.flags & VAS_EBOOT_NTFS_AF_ALST) == 0)
mft->attr.attr_end = 0; /* Don't jump to attribute list */
}
else
return init_attr (&mft->attr, mft);
return 0;
}
static void
free_file (struct VasEBoot_ntfs_file *mft)
{
if (mft)
{
free_attr (&mft->attr);
VasEBoot_free (mft->buf);
}
}
static char *
get_utf8 (VasEBoot_uint8_t *in, VasEBoot_size_t len)
{
VasEBoot_uint8_t *buf;
VasEBoot_uint16_t *tmp;
VasEBoot_size_t i;
buf = VasEBoot_calloc (len, VAS_EBOOT_MAX_UTF8_PER_UTF16 + 1);
tmp = VasEBoot_calloc (len, sizeof (tmp[0]));
if (!buf || !tmp)
{
VasEBoot_free (buf);
VasEBoot_free (tmp);
return NULL;
}
for (i = 0; i < len; i++)
tmp[i] = VasEBoot_le_to_cpu16 (VasEBoot_get_unaligned16 (in + 2 * i));
*VasEBoot_utf16_to_utf8 (buf, tmp, len) = '\0';
VasEBoot_free (tmp);
return (char *) buf;
}
static int
list_file (struct VasEBoot_ntfs_file *diro, VasEBoot_uint8_t *pos, VasEBoot_uint8_t *end_pos,
VasEBoot_fshelp_iterate_dir_hook_t hook, void *hook_data)
{
VasEBoot_uint8_t *np;
int ns;
VasEBoot_uint16_t pos_incr;
while (1)
{
VasEBoot_uint8_t namespace;
char *ustr;
if ((pos >= end_pos) || (end_pos - pos < 0x52))
break;
if (pos[0xC] & 2) /* end signature */
break;
np = pos + 0x50;
ns = *(np++);
namespace = *(np++);
if (2 * ns > end_pos - pos - 0x52)
break;
/*
* Ignore files in DOS namespace, as they will reappear as Win32
* names.
*/
if ((ns) && (namespace != 2))
{
enum VasEBoot_fshelp_filetype type;
struct VasEBoot_ntfs_file *fdiro;
VasEBoot_uint32_t attr;
attr = u32at (pos, 0x48);
if (attr & VAS_EBOOT_NTFS_ATTR_REPARSE)
type = VAS_EBOOT_FSHELP_SYMLINK;
else if (attr & VAS_EBOOT_NTFS_ATTR_DIRECTORY)
type = VAS_EBOOT_FSHELP_DIR;
else
type = VAS_EBOOT_FSHELP_REG;
fdiro = VasEBoot_zalloc (sizeof (struct VasEBoot_ntfs_file));
if (!fdiro)
return 0;
fdiro->data = diro->data;
fdiro->ino = u64at (pos, 0) & 0xffffffffffffULL;
fdiro->mtime = u64at (pos, 0x20);
ustr = get_utf8 (np, ns);
if (ustr == NULL)
{
VasEBoot_free (fdiro);
return 0;
}
if (namespace)
type |= VAS_EBOOT_FSHELP_CASE_INSENSITIVE;
if (hook (ustr, type, fdiro, hook_data))
{
VasEBoot_free (ustr);
return 1;
}
VasEBoot_free (ustr);
}
pos_incr = u16at (pos, 8);
if (pos_incr > 0)
pos += pos_incr;
else
return 0;
}
return 0;
}
struct symlink_descriptor
{
VasEBoot_uint32_t type;
VasEBoot_uint32_t total_len;
VasEBoot_uint16_t off1;
VasEBoot_uint16_t len1;
VasEBoot_uint16_t off2;
VasEBoot_uint16_t len2;
} VAS_EBOOT_PACKED;
static char *
VasEBoot_ntfs_read_symlink (VasEBoot_fshelp_node_t node)
{
struct VasEBoot_ntfs_file *mft;
struct symlink_descriptor symdesc;
VasEBoot_err_t err;
VasEBoot_uint8_t *buf16 = NULL;
char *buf, *end;
VasEBoot_size_t len;
VasEBoot_uint8_t *pa;
VasEBoot_size_t off;
mft = (struct VasEBoot_ntfs_file *) node;
mft->buf = VasEBoot_malloc (mft->data->mft_size << VAS_EBOOT_NTFS_BLK_SHR);
if (mft->buf == NULL)
return NULL;
if (read_mft (mft->data, mft->buf, mft->ino))
goto fail;
pa = locate_attr (&mft->attr, mft, VAS_EBOOT_NTFS_AT_SYMLINK);
if (pa == NULL)
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "no $SYMLINK in MFT 0x%llx",
(unsigned long long) mft->ino);
goto fail;
}
err = read_attr (&mft->attr, (VasEBoot_uint8_t *) &symdesc, 0,
sizeof (struct symlink_descriptor), 1, 0, 0);
if (err)
goto fail;
switch (VasEBoot_cpu_to_le32 (symdesc.type))
{
case 0xa000000c:
off = (sizeof (struct symlink_descriptor) + 4
+ VasEBoot_cpu_to_le32 (symdesc.off1));
len = VasEBoot_cpu_to_le32 (symdesc.len1);
break;
case 0xa0000003:
off = (sizeof (struct symlink_descriptor)
+ VasEBoot_cpu_to_le32 (symdesc.off1));
len = VasEBoot_cpu_to_le32 (symdesc.len1);
break;
default:
VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "symlink type invalid (%x)",
VasEBoot_cpu_to_le32 (symdesc.type));
goto fail;
}
buf16 = VasEBoot_malloc (len);
if (!buf16)
goto fail;
err = read_attr (&mft->attr, buf16, off, len, 1, 0, 0);
if (err)
goto fail;
buf = get_utf8 (buf16, len / 2);
if (!buf)
goto fail;
VasEBoot_free (mft->buf);
VasEBoot_free (buf16);
for (end = buf; *end; end++)
if (*end == '\\')
*end = '/';
/* Split the sequence to avoid GCC thinking that this is a trigraph. */
if (VasEBoot_memcmp (buf, "/?" "?/", 4) == 0 && buf[5] == ':' && buf[6] == '/'
&& VasEBoot_isalpha (buf[4]))
{
VasEBoot_memmove (buf, buf + 6, end - buf + 1 - 6);
end -= 6;
}
return buf;
fail:
VasEBoot_free (mft->buf);
VasEBoot_free (buf16);
return NULL;
}
static int
VasEBoot_ntfs_iterate_dir (VasEBoot_fshelp_node_t dir,
VasEBoot_fshelp_iterate_dir_hook_t hook, void *hook_data)
{
VasEBoot_uint8_t *bitmap;
struct VasEBoot_ntfs_attr attr, *at;
VasEBoot_uint8_t *cur_pos, *indx, *bmp;
int ret = 0;
VasEBoot_size_t bitmap_len;
struct VasEBoot_ntfs_file *mft;
/* Used to make sure we're not stuck in a loop. */
VasEBoot_uint8_t *last_pos = NULL;
VasEBoot_uint32_t tmp_len;
mft = (struct VasEBoot_ntfs_file *) dir;
if (!mft->inode_read)
{
if (init_file (mft, mft->ino))
return 0;
}
indx = NULL;
bmp = NULL;
at = &attr;
if (init_attr (at, mft) != VAS_EBOOT_ERR_NONE)
return 0;
while (1)
{
cur_pos = find_attr (at, VAS_EBOOT_NTFS_AT_INDEX_ROOT);
if (cur_pos == NULL || cur_pos == last_pos)
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "no $INDEX_ROOT");
goto done;
}
last_pos = cur_pos;
/* Resident, Namelen=4, Offset=0x18, Flags=0x00, Name="$I30" */
if ((u32at (cur_pos, 8) != 0x180400) ||
(u32at (cur_pos, 0x18) != 0x490024) ||
(u32at (cur_pos, 0x1C) != 0x300033))
continue;
cur_pos += res_attr_data_off (cur_pos);
if(cur_pos >= at->end)
continue;
if (*cur_pos != 0x30) /* Not filename index */
continue;
break;
}
cur_pos += 0x10; /* Skip index root */
ret = list_file (mft, cur_pos + u16at (cur_pos, 0),
at->mft->buf + (at->mft->data->mft_size << VAS_EBOOT_NTFS_BLK_SHR),
hook, hook_data);
if (ret)
goto done;
bitmap = NULL;
bitmap_len = 0;
free_attr (at);
/* No need to check errors here, as it will already be fine */
init_attr (at, mft);
last_pos = NULL;
while ((cur_pos = find_attr (at, VAS_EBOOT_NTFS_AT_BITMAP)) != NULL)
{
int ofs;
if (cur_pos == last_pos)
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "bitmap attribute loop");
goto done;
}
last_pos = cur_pos;
ofs = cur_pos[0xA];
/* Namelen=4, Name="$I30" */
if ((cur_pos[9] == 4) &&
(u32at (cur_pos, ofs) == 0x490024) &&
(u32at (cur_pos, ofs + 4) == 0x300033))
{
int is_resident = (cur_pos[8] == 0);
bitmap_len = ((is_resident) ? res_attr_data_len (cur_pos) :
u32at (cur_pos, 0x28));
bmp = VasEBoot_malloc (bitmap_len);
if (bmp == NULL)
goto done;
if (is_resident)
{
if (bitmap_len > (at->mft->data->mft_size << VAS_EBOOT_NTFS_BLK_SHR))
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "resident bitmap too large");
goto done;
}
if (cur_pos >= at->mft->buf + (at->mft->data->mft_size << VAS_EBOOT_NTFS_BLK_SHR))
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "resident bitmap out of range");
goto done;
}
if (res_attr_data_off (cur_pos) + res_attr_data_len (cur_pos) >
(VasEBoot_addr_t) at->mft->buf + (at->mft->data->mft_size << VAS_EBOOT_NTFS_BLK_SHR) - (VasEBoot_addr_t) cur_pos)
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "resident bitmap out of range");
goto done;
}
VasEBoot_memcpy (bmp, cur_pos + res_attr_data_off (cur_pos),
bitmap_len);
}
else
{
if (read_data (at, cur_pos, bmp, 0, bitmap_len, 0, 0, 0))
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_FS,
"fails to read non-resident $BITMAP");
goto done;
}
tmp_len = u32at (cur_pos, 0x30);
if (tmp_len <= bitmap_len)
bitmap_len = tmp_len;
else
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_FS,
"bitmap len too large for non-resident $BITMAP");
goto done;
}
}
bitmap = bmp;
break;
}
}
free_attr (at);
last_pos = NULL;
cur_pos = locate_attr (at, mft, VAS_EBOOT_NTFS_AT_INDEX_ALLOCATION);
while (cur_pos != NULL)
{
/* Non-resident, Namelen=4, Offset=0x40, Flags=0, Name="$I30" */
if ((u32at (cur_pos, 8) == 0x400401) &&
(u32at (cur_pos, 0x40) == 0x490024) &&
(u32at (cur_pos, 0x44) == 0x300033))
break;
cur_pos = find_attr (at, VAS_EBOOT_NTFS_AT_INDEX_ALLOCATION);
if (cur_pos == last_pos)
break;
last_pos = cur_pos;
}
if ((!cur_pos) && (bitmap))
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "$BITMAP without $INDEX_ALLOCATION");
goto done;
}
if (bitmap)
{
VasEBoot_disk_addr_t i;
VasEBoot_uint8_t v;
indx = VasEBoot_malloc (mft->data->idx_size << VAS_EBOOT_NTFS_BLK_SHR);
if (indx == NULL)
goto done;
v = 1;
for (i = 0; i < (VasEBoot_disk_addr_t)bitmap_len * 8; i++)
{
if (*bitmap & v)
{
if ((read_attr
(at, indx, i * (mft->data->idx_size << VAS_EBOOT_NTFS_BLK_SHR),
(mft->data->idx_size << VAS_EBOOT_NTFS_BLK_SHR), 0, 0, 0))
|| (fixup (indx, mft->data->idx_size,
(const VasEBoot_uint8_t *) "INDX")))
goto done;
ret = list_file (mft, &indx[0x18 + u16at (indx, 0x18)],
indx + (mft->data->idx_size << VAS_EBOOT_NTFS_BLK_SHR),
hook, hook_data);
if (ret)
goto done;
}
v <<= 1;
if (!v)
{
v = 1;
bitmap++;
}
}
}
done:
free_attr (at);
VasEBoot_free (indx);
VasEBoot_free (bmp);
return ret;
}
static struct VasEBoot_ntfs_data *
VasEBoot_ntfs_mount (VasEBoot_disk_t disk)
{
struct VasEBoot_ntfs_bpb bpb;
struct VasEBoot_ntfs_data *data = 0;
VasEBoot_uint32_t spc;
if (!disk)
goto fail;
data = (struct VasEBoot_ntfs_data *) VasEBoot_zalloc (sizeof (*data));
if (!data)
goto fail;
data->disk = disk;
/* Read the BPB. */
if (VasEBoot_disk_read (disk, 0, 0, sizeof (bpb), &bpb))
goto fail;
if (VasEBoot_memcmp ((char *) &bpb.oem_name, "NTFS", 4) != 0
|| bpb.sectors_per_cluster == 0
|| (bpb.sectors_per_cluster & (bpb.sectors_per_cluster - 1)) != 0
|| bpb.bytes_per_sector == 0
|| (bpb.bytes_per_sector & (bpb.bytes_per_sector - 1)) != 0)
goto fail;
spc = (((VasEBoot_uint32_t) bpb.sectors_per_cluster
* (VasEBoot_uint32_t) VasEBoot_le_to_cpu16 (bpb.bytes_per_sector))
>> VAS_EBOOT_NTFS_BLK_SHR);
if (spc == 0)
goto fail;
for (data->log_spc = 0; (1U << data->log_spc) < spc; data->log_spc++);
if (bpb.clusters_per_mft > 0)
data->mft_size = ((VasEBoot_disk_addr_t) bpb.clusters_per_mft) << data->log_spc;
else if (-bpb.clusters_per_mft < VAS_EBOOT_NTFS_BLK_SHR || -bpb.clusters_per_mft >= 31)
goto fail;
else
data->mft_size = 1ULL << (-bpb.clusters_per_mft - VAS_EBOOT_NTFS_BLK_SHR);
if (bpb.clusters_per_index > 0)
data->idx_size = (((VasEBoot_disk_addr_t) bpb.clusters_per_index)
<< data->log_spc);
else if (-bpb.clusters_per_index < VAS_EBOOT_NTFS_BLK_SHR || -bpb.clusters_per_index >= 31)
goto fail;
else
data->idx_size = 1ULL << (-bpb.clusters_per_index - VAS_EBOOT_NTFS_BLK_SHR);
data->mft_start = VasEBoot_le_to_cpu64 (bpb.mft_lcn) << data->log_spc;
if ((data->mft_size > VAS_EBOOT_NTFS_MAX_MFT) || (data->idx_size > VAS_EBOOT_NTFS_MAX_IDX))
goto fail;
data->mmft.data = data;
data->cmft.data = data;
data->mmft.buf = VasEBoot_malloc (data->mft_size << VAS_EBOOT_NTFS_BLK_SHR);
if (!data->mmft.buf)
goto fail;
if (VasEBoot_disk_read
(disk, data->mft_start, 0, data->mft_size << VAS_EBOOT_NTFS_BLK_SHR, data->mmft.buf))
goto fail;
data->uuid = VasEBoot_le_to_cpu64 (bpb.num_serial);
if (fixup (data->mmft.buf, data->mft_size, (const VasEBoot_uint8_t *) "FILE"))
goto fail;
if (!locate_attr (&data->mmft.attr, &data->mmft, VAS_EBOOT_NTFS_AT_DATA))
goto fail;
if (init_file (&data->cmft, VAS_EBOOT_NTFS_FILE_ROOT))
goto fail;
return data;
fail:
VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "not an ntfs filesystem");
if (data)
{
free_file (&data->mmft);
free_file (&data->cmft);
VasEBoot_free (data);
}
return 0;
}
/* Context for VasEBoot_ntfs_dir. */
struct VasEBoot_ntfs_dir_ctx
{
VasEBoot_fs_dir_hook_t hook;
void *hook_data;
};
/* Helper for VasEBoot_ntfs_dir. */
static int
VasEBoot_ntfs_dir_iter (const char *filename, enum VasEBoot_fshelp_filetype filetype,
VasEBoot_fshelp_node_t node, void *data)
{
struct VasEBoot_ntfs_dir_ctx *ctx = data;
struct VasEBoot_dirhook_info info;
VasEBoot_memset (&info, 0, sizeof (info));
info.dir = ((filetype & VAS_EBOOT_FSHELP_TYPE_MASK) == VAS_EBOOT_FSHELP_DIR);
info.mtimeset = 1;
info.mtime = VasEBoot_divmod64 (node->mtime, 10000000, 0)
- 86400ULL * 365 * (1970 - 1601)
- 86400ULL * ((1970 - 1601) / 4) + 86400ULL * ((1970 - 1601) / 100);
VasEBoot_free (node);
return ctx->hook (filename, &info, ctx->hook_data);
}
static VasEBoot_err_t
VasEBoot_ntfs_dir (VasEBoot_device_t device, const char *path,
VasEBoot_fs_dir_hook_t hook, void *hook_data)
{
struct VasEBoot_ntfs_dir_ctx ctx = { hook, hook_data };
struct VasEBoot_ntfs_data *data = 0;
struct VasEBoot_fshelp_node *fdiro = 0;
VasEBoot_dl_ref (my_mod);
data = VasEBoot_ntfs_mount (device->disk);
if (!data)
goto fail;
VasEBoot_fshelp_find_file (path, &data->cmft, &fdiro, VasEBoot_ntfs_iterate_dir,
VasEBoot_ntfs_read_symlink, VAS_EBOOT_FSHELP_DIR);
if (VasEBoot_errno)
goto fail;
VasEBoot_ntfs_iterate_dir (fdiro, VasEBoot_ntfs_dir_iter, &ctx);
fail:
if ((fdiro) && (fdiro != &data->cmft))
{
free_file (fdiro);
VasEBoot_free (fdiro);
}
if (data)
{
free_file (&data->mmft);
free_file (&data->cmft);
VasEBoot_free (data);
}
VasEBoot_dl_unref (my_mod);
return VasEBoot_errno;
}
static VasEBoot_err_t
VasEBoot_ntfs_open (VasEBoot_file_t file, const char *name)
{
struct VasEBoot_ntfs_data *data = 0;
struct VasEBoot_fshelp_node *mft = 0;
VasEBoot_dl_ref (my_mod);
data = VasEBoot_ntfs_mount (file->device->disk);
if (!data)
goto fail;
VasEBoot_fshelp_find_file (name, &data->cmft, &mft, VasEBoot_ntfs_iterate_dir,
VasEBoot_ntfs_read_symlink, VAS_EBOOT_FSHELP_REG);
if (VasEBoot_errno)
goto fail;
if (mft != &data->cmft)
{
free_file (&data->cmft);
VasEBoot_memcpy (&data->cmft, mft, sizeof (*mft));
VasEBoot_free (mft);
if (!data->cmft.inode_read)
{
if (init_file (&data->cmft, data->cmft.ino))
goto fail;
}
}
file->size = data->cmft.size;
file->data = data;
file->offset = 0;
return 0;
fail:
if (data)
{
free_file (&data->mmft);
free_file (&data->cmft);
VasEBoot_free (data);
}
VasEBoot_dl_unref (my_mod);
return VasEBoot_errno;
}
static VasEBoot_ssize_t
VasEBoot_ntfs_read (VasEBoot_file_t file, char *buf, VasEBoot_size_t len)
{
struct VasEBoot_ntfs_file *mft;
mft = &((struct VasEBoot_ntfs_data *) file->data)->cmft;
if (file->read_hook)
mft->attr.save_pos = 1;
read_attr (&mft->attr, (VasEBoot_uint8_t *) buf, file->offset, len, 1,
file->read_hook, file->read_hook_data);
return (VasEBoot_errno) ? -1 : (VasEBoot_ssize_t) len;
}
static VasEBoot_err_t
VasEBoot_ntfs_close (VasEBoot_file_t file)
{
struct VasEBoot_ntfs_data *data;
data = file->data;
if (data)
{
free_file (&data->mmft);
free_file (&data->cmft);
VasEBoot_free (data);
}
VasEBoot_dl_unref (my_mod);
return VasEBoot_errno;
}
static VasEBoot_err_t
VasEBoot_ntfs_label (VasEBoot_device_t device, char **label)
{
struct VasEBoot_ntfs_data *data = 0;
struct VasEBoot_fshelp_node *mft = 0;
VasEBoot_uint8_t *pa;
VasEBoot_err_t err;
VasEBoot_dl_ref (my_mod);
*label = 0;
data = VasEBoot_ntfs_mount (device->disk);
if (!data)
goto fail;
VasEBoot_fshelp_find_file ("/$Volume", &data->cmft, &mft, VasEBoot_ntfs_iterate_dir,
0, VAS_EBOOT_FSHELP_REG);
if (VasEBoot_errno)
goto fail;
if (!mft->inode_read)
{
mft->buf = VasEBoot_malloc (mft->data->mft_size << VAS_EBOOT_NTFS_BLK_SHR);
if (mft->buf == NULL)
goto fail;
if (read_mft (mft->data, mft->buf, mft->ino))
goto fail;
}
err = init_attr (&mft->attr, mft);
if (err != VAS_EBOOT_ERR_NONE)
return err;
pa = find_attr (&mft->attr, VAS_EBOOT_NTFS_AT_VOLUME_NAME);
if (pa == NULL || pa >= mft->buf + (mft->data->mft_size << VAS_EBOOT_NTFS_BLK_SHR))
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "can\'t parse volume label");
goto fail;
}
if (mft->buf + (mft->data->mft_size << VAS_EBOOT_NTFS_BLK_SHR) - pa < 0x16)
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "can\'t parse volume label");
goto fail;
}
if ((pa) && (pa[8] == 0) && (res_attr_data_len (pa)))
{
int len;
len = res_attr_data_len (pa) / 2;
pa += res_attr_data_off (pa);
if (mft->buf + (mft->data->mft_size << VAS_EBOOT_NTFS_BLK_SHR) - pa >= 2 * len &&
pa >= mft->buf && (pa + len < (mft->buf + (mft->data->mft_size << VAS_EBOOT_NTFS_BLK_SHR))))
*label = get_utf8 (pa, len);
else
VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "can\'t parse volume label");
}
fail:
if ((mft) && (mft != &data->cmft))
{
free_file (mft);
VasEBoot_free (mft);
}
if (data)
{
free_file (&data->mmft);
free_file (&data->cmft);
VasEBoot_free (data);
}
VasEBoot_dl_unref (my_mod);
return VasEBoot_errno;
}
static VasEBoot_err_t
VasEBoot_ntfs_uuid (VasEBoot_device_t device, char **uuid)
{
struct VasEBoot_ntfs_data *data;
VasEBoot_disk_t disk = device->disk;
VasEBoot_dl_ref (my_mod);
data = VasEBoot_ntfs_mount (disk);
if (data)
{
char *ptr;
*uuid = VasEBoot_xasprintf ("%016llx", (unsigned long long) data->uuid);
if (*uuid)
for (ptr = *uuid; *ptr; ptr++)
*ptr = VasEBoot_toupper (*ptr);
free_file (&data->mmft);
free_file (&data->cmft);
VasEBoot_free (data);
}
else
*uuid = NULL;
VasEBoot_dl_unref (my_mod);
return VasEBoot_errno;
}
static struct VasEBoot_fs VasEBoot_ntfs_fs =
{
.name = "ntfs",
.fs_dir = VasEBoot_ntfs_dir,
.fs_open = VasEBoot_ntfs_open,
.fs_read = VasEBoot_ntfs_read,
.fs_close = VasEBoot_ntfs_close,
.fs_label = VasEBoot_ntfs_label,
.fs_uuid = VasEBoot_ntfs_uuid,
#ifdef VAS_EBOOT_UTIL
.reserved_first_sector = 1,
.blocklist_install = 1,
#endif
.next = 0
};
VAS_EBOOT_MOD_INIT (ntfs)
{
if (!VasEBoot_is_lockdown ())
{
VasEBoot_ntfs_fs.mod = mod;
VasEBoot_fs_register (&VasEBoot_ntfs_fs);
}
my_mod = mod;
}
VAS_EBOOT_MOD_FINI (ntfs)
{
if (!VasEBoot_is_lockdown ())
VasEBoot_fs_unregister (&VasEBoot_ntfs_fs);
}