/*
* Copyright (c) 2007, 2008 Reinoud Zandijk
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
void
udf_fixup_fid_block(uint8_t *blob, int lb_size,
int rfix_pos, int max_rfix_pos, uint32_t lb_num)
{
struct fileid_desc *fid;
uint8_t *fid_pos;
int fid_len, found;
/* needs to be word aligned */
KASSERT(rfix_pos % 4 == 0);
/* first resync with the FID stream !!! */
found = 0;
while (rfix_pos + sizeof(struct desc_tag) <= max_rfix_pos) {
fid_pos = blob + rfix_pos;
fid = (struct fileid_desc *) fid_pos;
if (udf_rw16(fid->tag.id) == TAGID_FID) {
if (udf_check_tag((union dscrptr *) fid) == 0)
found = 1;
}
if (found)
break;
/* try next location; can only be 4 bytes aligned */
rfix_pos += 4;
}
/* walk over the fids */
fid_pos = blob + rfix_pos;
while (rfix_pos + sizeof(struct desc_tag) <= max_rfix_pos) {
fid = (struct fileid_desc *) fid_pos;
if (udf_rw16(fid->tag.id) != TAGID_FID) {
/* end of FID stream; end of directory or currupted */
break;
}
/* update sector number and recalculate checksum */
fid->tag.tag_loc = udf_rw32(lb_num);
udf_validate_tag_sum((union dscrptr *) fid);
/* if the FID crosses the memory, we're done! */
if (rfix_pos + UDF_FID_SIZE >= max_rfix_pos)
break;
/* get information from fe/efe */
tag = (struct desc_tag *) blob;
switch (udf_rw16(tag->id)) {
case TAGID_FENTRY :
fe = (struct file_entry *) blob;
l_ea = udf_rw32(fe->l_ea);
eahdr = (struct extattrhdr_desc *) fe->data;
break;
case TAGID_EXTFENTRY :
efe = (struct extfile_entry *) blob;
l_ea = udf_rw32(efe->l_ea);
eahdr = (struct extattrhdr_desc *) efe->data;
break;
case TAGID_INDIRECTENTRY :
case TAGID_ALLOCEXTENT :
case TAGID_EXTATTR_HDR :
return;
default:
panic("%s: passed bad tag\n", __func__);
}
/* something recorded here? (why am i called?) */
if (l_ea == 0)
return;
#if 0
/* check extended attribute tag */
/* TODO XXX what to do when we encounter an error here? */
error = udf_check_tag(eahdr);
if (error)
return; /* for now */
if (udf_rw16(eahdr->tag.id) != TAGID_EXTATTR_HDR)
return; /* for now */
error = udf_check_tag_payload(eahdr, sizeof(struct extattrhdr_desc));
if (error)
return; /* for now */
#endif
DPRINTF(EXTATTR, ("node fixup: found %d bytes of extended attributes\n",
l_ea));
/* determine what to fix if its internally recorded */
if (intern) {
has_fids = (file_type == UDF_ICB_FILETYPE_DIRECTORY) ||
(file_type == UDF_ICB_FILETYPE_STREAMDIR);
has_sbm = (file_type == UDF_ICB_FILETYPE_META_BITMAP);
}
/* fixup internal extended attributes if present */
if (l_ea)
udf_fixup_internal_extattr(blob, lb_num);
/*
* Set of generic descriptor readers and writers and their helper functions.
* Descriptors inside `logical space' i.e. inside logically mapped partitions
* can never be longer than one logical sector.
*
* NOTE that these functions *can* be used by the scheduler backends to read
* node descriptors too.
*
* For reading, the size of allocated piece is returned in multiple of sector
* size due to udf_calc_udf_malloc_size().
*/
/* SYNC reading of n blocks from specified sector */
int
udf_read_phys_sectors(struct udf_mount *ump, int what, void *blob,
uint32_t start, uint32_t sectors)
{
struct buf *buf, *nestbuf;
uint32_t buf_offset;
off_t lblkno, rblkno;
int sector_size = ump->discinfo.sector_size;
int blks = sector_size / DEV_BSIZE;
int piece;
int error;
if (!error) {
/* check if its a valid tag */
error = udf_check_tag(dst);
if (error) {
/* check if its an empty block */
pos = (uint8_t *) dst;
for (i = 0; i < sector_size; i++, pos++) {
if (*pos) break;
}
if (i == sector_size) {
/* return no error but with no dscrptr */
/* dispose first block */
free(dst, mtype);
return 0;
}
}
/* calculate descriptor size */
dscrlen = udf_tagsize(dst, sector_size);
}
DPRINTFIF(DESCRIPTOR, error, ("bad tag checksum\n"));
if (!error && (dscrlen > sector_size)) {
DPRINTF(DESCRIPTOR, ("multi block descriptor read\n"));
/*
* Read the rest of descriptor. Since it is only used at mount
* time its overdone to define and use a specific udf_intbreadn
* for this alone.
*/