^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * misc.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * PURPOSE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Miscellaneous routines for the OSTA-UDF(tm) filesystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * COPYRIGHT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * This file is distributed under the terms of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * License (GPL). Copies of the GPL can be obtained from:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * ftp://prep.ai.mit.edu/pub/gnu/GPL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Each contributing author retains all rights to their own work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * (C) 1998 Dave Boynton
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * (C) 1998-2004 Ben Fennema
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * (C) 1999-2000 Stelias Computing Inc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * HISTORY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * 04/19/99 blf partial support for reading/writing specific EA's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "udfdecl.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/crc-itu-t.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include "udf_i.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include "udf_sb.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct buffer_head *udf_tgetblk(struct super_block *sb, udf_pblk_t block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return sb_getblk(sb, udf_fixed_to_variable(block));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return sb_getblk(sb, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct buffer_head *udf_tread(struct super_block *sb, udf_pblk_t block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return sb_bread(sb, udf_fixed_to_variable(block));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return sb_bread(sb, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct genericFormat *udf_add_extendedattr(struct inode *inode, uint32_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) uint32_t type, uint8_t loc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) uint8_t *ea = NULL, *ad = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) uint16_t crclen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct udf_inode_info *iinfo = UDF_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) ea = iinfo->i_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (iinfo->i_lenEAttr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) ad = iinfo->i_data + iinfo->i_lenEAttr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) ad = ea;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) size += sizeof(struct extendedAttrHeaderDesc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) offset = inode->i_sb->s_blocksize - udf_file_entry_alloc_offset(inode) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) iinfo->i_lenAlloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) /* TODO - Check for FreeEASpace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (loc & 0x01 && offset >= size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct extendedAttrHeaderDesc *eahd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) eahd = (struct extendedAttrHeaderDesc *)ea;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (iinfo->i_lenAlloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) memmove(&ad[size], ad, iinfo->i_lenAlloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (iinfo->i_lenEAttr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) /* check checksum/crc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (eahd->descTag.tagIdent !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) cpu_to_le16(TAG_IDENT_EAHD) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) le32_to_cpu(eahd->descTag.tagLocation) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) iinfo->i_location.logicalBlockNum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) size -= sizeof(struct extendedAttrHeaderDesc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) iinfo->i_lenEAttr +=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) sizeof(struct extendedAttrHeaderDesc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) eahd->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EAHD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (sbi->s_udfrev >= 0x0200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) eahd->descTag.descVersion = cpu_to_le16(3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) eahd->descTag.descVersion = cpu_to_le16(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) eahd->descTag.tagSerialNum =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) cpu_to_le16(sbi->s_serial_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) eahd->descTag.tagLocation = cpu_to_le32(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) iinfo->i_location.logicalBlockNum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) eahd->impAttrLocation = cpu_to_le32(0xFFFFFFFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) eahd->appAttrLocation = cpu_to_le32(0xFFFFFFFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) offset = iinfo->i_lenEAttr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (type < 2048) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (le32_to_cpu(eahd->appAttrLocation) <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) iinfo->i_lenEAttr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) uint32_t aal =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) le32_to_cpu(eahd->appAttrLocation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) memmove(&ea[offset - aal + size],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) &ea[aal], offset - aal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) offset -= aal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) eahd->appAttrLocation =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) cpu_to_le32(aal + size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (le32_to_cpu(eahd->impAttrLocation) <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) iinfo->i_lenEAttr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) uint32_t ial =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) le32_to_cpu(eahd->impAttrLocation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) memmove(&ea[offset - ial + size],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) &ea[ial], offset - ial);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) offset -= ial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) eahd->impAttrLocation =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) cpu_to_le32(ial + size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) } else if (type < 65536) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (le32_to_cpu(eahd->appAttrLocation) <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) iinfo->i_lenEAttr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) uint32_t aal =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) le32_to_cpu(eahd->appAttrLocation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) memmove(&ea[offset - aal + size],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) &ea[aal], offset - aal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) offset -= aal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) eahd->appAttrLocation =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) cpu_to_le32(aal + size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) /* rewrite CRC + checksum of eahd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) crclen = sizeof(struct extendedAttrHeaderDesc) - sizeof(struct tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) eahd->descTag.descCRCLength = cpu_to_le16(crclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) eahd->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)eahd +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) sizeof(struct tag), crclen));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) eahd->descTag.tagChecksum = udf_tag_checksum(&eahd->descTag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) iinfo->i_lenEAttr += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return (struct genericFormat *)&ea[offset];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct genericFormat *udf_get_extendedattr(struct inode *inode, uint32_t type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) uint8_t subtype)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct genericFormat *gaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) uint8_t *ea = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) uint32_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct udf_inode_info *iinfo = UDF_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) ea = iinfo->i_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (iinfo->i_lenEAttr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) struct extendedAttrHeaderDesc *eahd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) eahd = (struct extendedAttrHeaderDesc *)ea;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) /* check checksum/crc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (eahd->descTag.tagIdent !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) cpu_to_le16(TAG_IDENT_EAHD) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) le32_to_cpu(eahd->descTag.tagLocation) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) iinfo->i_location.logicalBlockNum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (type < 2048)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) offset = sizeof(struct extendedAttrHeaderDesc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) else if (type < 65536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) offset = le32_to_cpu(eahd->impAttrLocation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) offset = le32_to_cpu(eahd->appAttrLocation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) while (offset + sizeof(*gaf) < iinfo->i_lenEAttr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) uint32_t attrLength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) gaf = (struct genericFormat *)&ea[offset];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) attrLength = le32_to_cpu(gaf->attrLength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) /* Detect undersized elements and buffer overflows */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if ((attrLength < sizeof(*gaf)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) (attrLength > (iinfo->i_lenEAttr - offset)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (le32_to_cpu(gaf->attrType) == type &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) gaf->attrSubtype == subtype)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) return gaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) offset += attrLength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * udf_read_tagged
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * PURPOSE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * Read the first block of a tagged descriptor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * HISTORY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * July 1, 1997 - Andrew E. Mileski
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * Written, tested, and released.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) struct buffer_head *udf_read_tagged(struct super_block *sb, uint32_t block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) uint32_t location, uint16_t *ident)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) struct tag *tag_p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) struct buffer_head *bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) u8 checksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) /* Read the block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (block == 0xFFFFFFFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) bh = udf_tread(sb, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (!bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) udf_err(sb, "read failed, block=%u, location=%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) block, location);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) tag_p = (struct tag *)(bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) *ident = le16_to_cpu(tag_p->tagIdent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (location != le32_to_cpu(tag_p->tagLocation)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) udf_debug("location mismatch block %u, tag %u != %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) block, le32_to_cpu(tag_p->tagLocation), location);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) goto error_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) /* Verify the tag checksum */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) checksum = udf_tag_checksum(tag_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (checksum != tag_p->tagChecksum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) udf_err(sb, "tag checksum failed, block %u: 0x%02x != 0x%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) block, checksum, tag_p->tagChecksum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) goto error_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) /* Verify the tag version */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (tag_p->descVersion != cpu_to_le16(0x0002U) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) tag_p->descVersion != cpu_to_le16(0x0003U)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) udf_err(sb, "tag version 0x%04x != 0x0002 || 0x0003, block %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) le16_to_cpu(tag_p->descVersion), block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) goto error_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) /* Verify the descriptor CRC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) if (le16_to_cpu(tag_p->descCRCLength) + sizeof(struct tag) > sb->s_blocksize ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) le16_to_cpu(tag_p->descCRC) == crc_itu_t(0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) bh->b_data + sizeof(struct tag),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) le16_to_cpu(tag_p->descCRCLength)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) udf_debug("Crc failure block %u: crc = %u, crclen = %u\n", block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) le16_to_cpu(tag_p->descCRC),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) le16_to_cpu(tag_p->descCRCLength));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) error_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) struct buffer_head *udf_read_ptagged(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) struct kernel_lb_addr *loc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) uint32_t offset, uint16_t *ident)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return udf_read_tagged(sb, udf_get_lb_pblock(sb, loc, offset),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) loc->logicalBlockNum + offset, ident);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) void udf_update_tag(char *data, int length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) struct tag *tptr = (struct tag *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) length -= sizeof(struct tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) tptr->descCRCLength = cpu_to_le16(length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) tptr->descCRC = cpu_to_le16(crc_itu_t(0, data + sizeof(struct tag), length));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) tptr->tagChecksum = udf_tag_checksum(tptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) void udf_new_tag(char *data, uint16_t ident, uint16_t version, uint16_t snum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) uint32_t loc, int length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) struct tag *tptr = (struct tag *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) tptr->tagIdent = cpu_to_le16(ident);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) tptr->descVersion = cpu_to_le16(version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) tptr->tagSerialNum = cpu_to_le16(snum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) tptr->tagLocation = cpu_to_le32(loc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) udf_update_tag(data, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) u8 udf_tag_checksum(const struct tag *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) u8 *data = (u8 *)t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) u8 checksum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) for (i = 0; i < sizeof(struct tag); ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (i != 4) /* position of checksum */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) checksum += data[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) return checksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }