4 * Read, write, or create a WIM header.
8 * Copyright (C) 2012, 2013 Eric Biggers
10 * This file is part of wimlib, a library for working with WIM files.
12 * wimlib is free software; you can redistribute it and/or modify it under the
13 * terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your option)
17 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
18 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 * A PARTICULAR PURPOSE. See the GNU General Public License for more
22 * You should have received a copy of the GNU General Public License
23 * along with wimlib; if not, see http://www.gnu.org/licenses/.
31 #include "wimlib/assert.h"
32 #include "wimlib/endianness.h"
33 #include "wimlib/error.h"
34 #include "wimlib/file_io.h"
35 #include "wimlib/header.h"
36 #include "wimlib/util.h"
37 #include "wimlib/wim.h"
48 /* On-disk format of the WIM header. */
49 struct wim_header_disk {
51 /* Magic characters "MSWIM\0\0\0" */
54 /* Size of the WIM header, in bytes; WIM_HEADER_DISK_SIZE expected
55 * (currently the only supported value). */
58 /* Version of the WIM file; WIM_VERSION expected (currently the only
59 * supported value). */
62 /* Flags for the WIM file (WIM_HDR_FLAG_*) */
65 /* Uncompressed chunk size of resources in the WIM. 0 if the WIM is
66 * uncompressed. If compressed, WIM_CHUNK_SIZE is expected (currently
67 * the only supported value). */
70 /* Globally unique identifier for the WIM file. Basically a bunch of
74 /* Number of this WIM part in the split WIM file, indexed from 1, or 1
75 * if the WIM is not split. */
78 /* Total number of parts of the split WIM file, or 1 if the WIM is not
82 /* Number of images in the WIM. */
85 /* Location and size of the WIM's lookup table. */
86 struct resource_entry_disk lookup_table_res_entry;
88 /* Location and size of the WIM's XML data. */
89 struct resource_entry_disk xml_data_res_entry;
91 /* Location and size of metadata resource for the bootable image of the
92 * WIM, or all zeroes if no image is bootable. */
93 struct resource_entry_disk boot_metadata_res_entry;
95 /* 1-based index of the bootable image of the WIM, or 0 if no image is
99 /* Location and size of the WIM's integrity table, or all zeroes if the
100 * WIM has no integrity table.
102 * Note the integrity_table_res_entry here is 4-byte aligned even though
103 * it would ordinarily be 8-byte aligned--- hence, the _packed_attribute
104 * on the `struct wim_header_disk' is essential. */
105 struct resource_entry_disk integrity_table_res_entry;
112 * Reads the header from a WIM file.
115 * Name of the WIM file (for error/debug messages only), or NULL if reading
116 * directly from file descriptor.
118 * File descriptor, positioned at offset 0, to read the header from.
120 * Structure to read the header into.
123 * WIMLIB_ERR_SUCCESS (0)
124 * WIMLIB_ERR_IMAGE_COUNT
125 * WIMLIB_ERR_INVALID_CHUNK_SIZE
126 * WIMLIB_ERR_INVALID_PART_NUMBER
127 * WIMLIB_ERR_NOT_A_WIM_FILE
129 * WIMLIB_ERR_UNEXPECTED_END_OF_FILE
130 * WIMLIB_ERR_UNKNOWN_VERSION
133 read_wim_header(const tchar *filename, struct filedes *in_fd,
134 struct wim_header *hdr)
136 struct wim_header_disk disk_hdr _aligned_attribute(8);
140 wimlib_assert(in_fd->offset == 0);
143 pipe_str = alloca(40);
144 tsprintf(pipe_str, T("[fd %d]"), in_fd->fd);
148 BUILD_BUG_ON(sizeof(struct wim_header_disk) != WIM_HEADER_DISK_SIZE);
150 DEBUG("Reading WIM header from \"%"TS"\"", filename);
152 ret = full_read(in_fd, &disk_hdr, sizeof(disk_hdr));
156 if (disk_hdr.magic != WIM_MAGIC) {
157 if (disk_hdr.magic == PWM_MAGIC) {
158 /* Pipable WIM: Use header at end instead, unless
159 * actually reading from a pipe. */
160 if (!in_fd->is_pipe) {
161 lseek(in_fd->fd, -WIM_HEADER_DISK_SIZE, SEEK_END);
162 ret = full_read(in_fd, &disk_hdr, sizeof(disk_hdr));
167 ERROR("\"%"TS"\": Invalid magic characters in header", filename);
168 return WIMLIB_ERR_NOT_A_WIM_FILE;
171 hdr->magic = disk_hdr.magic;
173 if (le32_to_cpu(disk_hdr.hdr_size) != sizeof(struct wim_header_disk)) {
174 ERROR("\"%"TS"\": Header size is invalid (%u bytes)",
175 filename, le32_to_cpu(disk_hdr.hdr_size));
176 return WIMLIB_ERR_INVALID_HEADER;
179 if (le32_to_cpu(disk_hdr.wim_version) != WIM_VERSION) {
180 ERROR("\"%"TS"\": The WIM header says the WIM version is %u, "
181 "but wimlib only knows about version %u",
182 filename, le32_to_cpu(disk_hdr.wim_version), WIM_VERSION);
183 return WIMLIB_ERR_UNKNOWN_VERSION;
186 hdr->flags = le32_to_cpu(disk_hdr.wim_flags);
187 if (le32_to_cpu(disk_hdr.chunk_size) != WIM_CHUNK_SIZE &&
188 (hdr->flags & WIM_HDR_FLAG_COMPRESSION)) {
189 ERROR("\"%"TS"\": Unexpected chunk size of %u! Ask the author to "
190 "implement support for other chunk sizes.",
191 filename, le32_to_cpu(disk_hdr.chunk_size));
192 ERROR("(Or it might just be that the WIM header is invalid.)");
193 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
196 memcpy(hdr->guid, disk_hdr.guid, WIM_GID_LEN);
198 hdr->part_number = le16_to_cpu(disk_hdr.part_number);
199 hdr->total_parts = le16_to_cpu(disk_hdr.total_parts);
201 if (hdr->total_parts == 0 || hdr->part_number == 0 ||
202 hdr->part_number > hdr->total_parts)
204 ERROR("\"%"TS"\": Invalid WIM part number: %hu of %hu",
205 filename, hdr->part_number, hdr->total_parts);
206 return WIMLIB_ERR_INVALID_PART_NUMBER;
209 hdr->image_count = le32_to_cpu(disk_hdr.image_count);
211 DEBUG("part_number = %u, total_parts = %u, image_count = %u",
212 hdr->part_number, hdr->total_parts, hdr->image_count);
214 if (hdr->image_count >= INT_MAX) {
215 ERROR("\"%"TS"\": Invalid image count (%u)",
216 filename, hdr->image_count);
217 return WIMLIB_ERR_IMAGE_COUNT;
220 get_resource_entry(&disk_hdr.lookup_table_res_entry, &hdr->lookup_table_res_entry);
221 get_resource_entry(&disk_hdr.xml_data_res_entry, &hdr->xml_res_entry);
222 get_resource_entry(&disk_hdr.boot_metadata_res_entry, &hdr->boot_metadata_res_entry);
223 hdr->boot_idx = le32_to_cpu(disk_hdr.boot_idx);
224 get_resource_entry(&disk_hdr.integrity_table_res_entry, &hdr->integrity);
228 ERROR_WITH_ERRNO("\"%"TS"\": Error reading header", filename);
232 /* Writes the header for a WIM file at the specified offset. If the offset
233 * specified is the current one, the position is advanced by the size of the
236 write_wim_header_at_offset(const struct wim_header *hdr, struct filedes *out_fd,
239 struct wim_header_disk disk_hdr _aligned_attribute(8);
242 DEBUG("Writing %sWIM header at offset %"PRIu64,
243 ((hdr->magic == PWM_MAGIC) ? "pipable " : ""),
246 disk_hdr.magic = hdr->magic;
247 disk_hdr.hdr_size = cpu_to_le32(sizeof(struct wim_header_disk));
248 disk_hdr.wim_version = cpu_to_le32(WIM_VERSION);
249 disk_hdr.wim_flags = cpu_to_le32(hdr->flags);
250 disk_hdr.chunk_size = cpu_to_le32((hdr->flags & WIM_HDR_FLAG_COMPRESSION) ?
252 memcpy(disk_hdr.guid, hdr->guid, WIM_GID_LEN);
254 disk_hdr.part_number = cpu_to_le16(hdr->part_number);
255 disk_hdr.total_parts = cpu_to_le16(hdr->total_parts);
256 disk_hdr.image_count = cpu_to_le32(hdr->image_count);
257 put_resource_entry(&hdr->lookup_table_res_entry, &disk_hdr.lookup_table_res_entry);
258 put_resource_entry(&hdr->xml_res_entry, &disk_hdr.xml_data_res_entry);
259 put_resource_entry(&hdr->boot_metadata_res_entry, &disk_hdr.boot_metadata_res_entry);
260 disk_hdr.boot_idx = cpu_to_le32(hdr->boot_idx);
261 put_resource_entry(&hdr->integrity, &disk_hdr.integrity_table_res_entry);
262 memset(disk_hdr.unused, 0, sizeof(disk_hdr.unused));
264 if (offset == out_fd->offset)
265 ret = full_write(out_fd, &disk_hdr, sizeof(disk_hdr));
267 ret = full_pwrite(out_fd, &disk_hdr, sizeof(disk_hdr), offset);
269 ERROR_WITH_ERRNO("Failed to write WIM header");
273 /* Writes the header for a WIM file at the output file descriptor's current
276 write_wim_header(const struct wim_header *hdr, struct filedes *out_fd)
278 return write_wim_header_at_offset(hdr, out_fd, out_fd->offset);
281 /* Update just the wim_flags field. */
283 write_wim_header_flags(u32 hdr_flags, struct filedes *out_fd)
285 le32 flags = cpu_to_le32(hdr_flags);
287 return full_pwrite(out_fd, &flags, sizeof(flags),
288 offsetof(struct wim_header_disk, wim_flags));
292 * Initializes the header for a WIM file.
295 init_wim_header(struct wim_header *hdr, int ctype)
297 memset(hdr, 0, sizeof(struct wim_header));
299 case WIMLIB_COMPRESSION_TYPE_NONE:
302 case WIMLIB_COMPRESSION_TYPE_LZX:
303 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
304 WIM_HDR_FLAG_COMPRESS_LZX;
306 case WIMLIB_COMPRESSION_TYPE_XPRESS:
307 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
308 WIM_HDR_FLAG_COMPRESS_XPRESS;
311 ERROR("Invalid compression type specified (%d)", ctype);
312 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
314 hdr->total_parts = 1;
315 hdr->part_number = 1;
316 randomize_byte_array(hdr->guid, sizeof(hdr->guid));
317 hdr->magic = WIM_MAGIC;
325 struct hdr_flag hdr_flags[] = {
326 {WIM_HDR_FLAG_RESERVED, "RESERVED"},
327 {WIM_HDR_FLAG_COMPRESSION, "COMPRESSION"},
328 {WIM_HDR_FLAG_READONLY, "READONLY"},
329 {WIM_HDR_FLAG_SPANNED, "SPANNED"},
330 {WIM_HDR_FLAG_RESOURCE_ONLY, "RESOURCE_ONLY"},
331 {WIM_HDR_FLAG_METADATA_ONLY, "METADATA_ONLY"},
332 {WIM_HDR_FLAG_WRITE_IN_PROGRESS,"WRITE_IN_PROGRESS"},
333 {WIM_HDR_FLAG_RP_FIX, "RP_FIX"},
334 {WIM_HDR_FLAG_COMPRESS_RESERVED,"COMPRESS_RESERVED"},
335 {WIM_HDR_FLAG_COMPRESS_LZX, "COMPRESS_LZX"},
336 {WIM_HDR_FLAG_COMPRESS_XPRESS, "COMPRESS_XPRESS"},
339 /* API function documented in wimlib.h */
341 wimlib_print_header(const WIMStruct *wim)
343 const struct wim_header *hdr = &wim->hdr;
345 tprintf(T("Magic Characters = MSWIM\\000\\000\\000\n"));
346 tprintf(T("Header Size = %u\n"), WIM_HEADER_DISK_SIZE);
347 tprintf(T("Version = 0x%x\n"), WIM_VERSION);
349 tprintf(T("Flags = 0x%x\n"), hdr->flags);
350 for (size_t i = 0; i < ARRAY_LEN(hdr_flags); i++)
351 if (hdr_flags[i].flag & hdr->flags)
352 tprintf(T(" WIM_HDR_FLAG_%s is set\n"), hdr_flags[i].name);
354 tprintf(T("Chunk Size = %u\n"), WIM_CHUNK_SIZE);
355 tfputs (T("GUID = "), stdout);
356 print_byte_field(hdr->guid, WIM_GID_LEN, stdout);
358 tprintf(T("Part Number = %hu\n"), wim->hdr.part_number);
359 tprintf(T("Total Parts = %hu\n"), wim->hdr.total_parts);
360 tprintf(T("Image Count = %u\n"), hdr->image_count);
361 tprintf(T("Lookup Table Size = %"PRIu64"\n"),
362 (u64)hdr->lookup_table_res_entry.size);
363 tprintf(T("Lookup Table Flags = 0x%hhx\n"),
364 (u8)hdr->lookup_table_res_entry.flags);
365 tprintf(T("Lookup Table Offset = %"PRIu64"\n"),
366 hdr->lookup_table_res_entry.offset);
367 tprintf(T("Lookup Table Original_size = %"PRIu64"\n"),
368 hdr->lookup_table_res_entry.original_size);
369 tprintf(T("XML Data Size = %"PRIu64"\n"),
370 (u64)hdr->xml_res_entry.size);
371 tprintf(T("XML Data Flags = 0x%hhx\n"),
372 (u8)hdr->xml_res_entry.flags);
373 tprintf(T("XML Data Offset = %"PRIu64"\n"),
374 hdr->xml_res_entry.offset);
375 tprintf(T("XML Data Original Size = %"PRIu64"\n"),
376 hdr->xml_res_entry.original_size);
377 tprintf(T("Boot Metadata Size = %"PRIu64"\n"),
378 (u64)hdr->boot_metadata_res_entry.size);
379 tprintf(T("Boot Metadata Flags = 0x%hhx\n"),
380 (u8)hdr->boot_metadata_res_entry.flags);
381 tprintf(T("Boot Metadata Offset = %"PRIu64"\n"),
382 hdr->boot_metadata_res_entry.offset);
383 tprintf(T("Boot Metadata Original Size = %"PRIu64"\n"),
384 hdr->boot_metadata_res_entry.original_size);
385 tprintf(T("Boot Index = %u\n"), hdr->boot_idx);
386 tprintf(T("Integrity Size = %"PRIu64"\n"),
387 (u64)hdr->integrity.size);
388 tprintf(T("Integrity Flags = 0x%hhx\n"),
389 (u8)hdr->integrity.flags);
390 tprintf(T("Integrity Offset = %"PRIu64"\n"),
391 hdr->integrity.offset);
392 tprintf(T("Integrity Original_size = %"PRIu64"\n"),
393 hdr->integrity.original_size);