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"
49 * Reads the header from a WIM file.
52 * Name of the WIM file (for error/debug messages only), or NULL if reading
53 * directly from file descriptor.
55 * File descriptor, positioned at offset 0, to read the header from.
57 * Structure to read the header into.
60 * WIMLIB_ERR_SUCCESS (0)
61 * WIMLIB_ERR_IMAGE_COUNT
62 * WIMLIB_ERR_INVALID_CHUNK_SIZE
63 * WIMLIB_ERR_INVALID_PART_NUMBER
64 * WIMLIB_ERR_NOT_A_WIM_FILE
66 * WIMLIB_ERR_UNEXPECTED_END_OF_FILE
67 * WIMLIB_ERR_UNKNOWN_VERSION
70 read_wim_header(const tchar *filename, struct filedes *in_fd,
71 struct wim_header *hdr)
73 struct wim_header_disk disk_hdr _aligned_attribute(8);
77 wimlib_assert(in_fd->offset == 0);
80 pipe_str = alloca(40);
81 tsprintf(pipe_str, T("[fd %d]"), in_fd->fd);
85 BUILD_BUG_ON(sizeof(struct wim_header_disk) != WIM_HEADER_DISK_SIZE);
87 DEBUG("Reading WIM header from \"%"TS"\"", filename);
89 ret = full_read(in_fd, &disk_hdr, sizeof(disk_hdr));
93 if (disk_hdr.magic != WIM_MAGIC) {
94 if (disk_hdr.magic == PWM_MAGIC) {
95 /* Pipable WIM: Use header at end instead, unless
96 * actually reading from a pipe. */
97 if (!in_fd->is_pipe) {
98 lseek(in_fd->fd, -WIM_HEADER_DISK_SIZE, SEEK_END);
99 ret = full_read(in_fd, &disk_hdr, sizeof(disk_hdr));
104 ERROR("\"%"TS"\": Invalid magic characters in header", filename);
105 return WIMLIB_ERR_NOT_A_WIM_FILE;
108 hdr->magic = disk_hdr.magic;
110 if (le32_to_cpu(disk_hdr.hdr_size) != sizeof(struct wim_header_disk)) {
111 ERROR("\"%"TS"\": Header size is invalid (%u bytes)",
112 filename, le32_to_cpu(disk_hdr.hdr_size));
113 return WIMLIB_ERR_INVALID_HEADER;
116 if (le32_to_cpu(disk_hdr.wim_version) != WIM_VERSION) {
117 ERROR("\"%"TS"\": The WIM header says the WIM version is %u, "
118 "but wimlib only knows about version %u",
119 filename, le32_to_cpu(disk_hdr.wim_version), WIM_VERSION);
120 if (le32_to_cpu(disk_hdr.wim_flags) & WIM_HDR_FLAG_COMPRESS_LZMS) {
121 ERROR("\"%"TS"\": This WIM uses LZMS compression, "
122 "which is not supported by wimlib.", filename);
124 return WIMLIB_ERR_UNKNOWN_VERSION;
127 hdr->flags = le32_to_cpu(disk_hdr.wim_flags);
128 if (le32_to_cpu(disk_hdr.chunk_size) != WIM_CHUNK_SIZE &&
129 (hdr->flags & WIM_HDR_FLAG_COMPRESSION)) {
130 ERROR("\"%"TS"\": Unexpected chunk size of %u! Ask the author to "
131 "implement support for other chunk sizes.",
132 filename, le32_to_cpu(disk_hdr.chunk_size));
133 ERROR("(Or it might just be that the WIM header is invalid.)");
134 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
137 memcpy(hdr->guid, disk_hdr.guid, WIM_GID_LEN);
139 hdr->part_number = le16_to_cpu(disk_hdr.part_number);
140 hdr->total_parts = le16_to_cpu(disk_hdr.total_parts);
142 if (hdr->total_parts == 0 || hdr->part_number == 0 ||
143 hdr->part_number > hdr->total_parts)
145 ERROR("\"%"TS"\": Invalid WIM part number: %hu of %hu",
146 filename, hdr->part_number, hdr->total_parts);
147 return WIMLIB_ERR_INVALID_PART_NUMBER;
150 hdr->image_count = le32_to_cpu(disk_hdr.image_count);
152 DEBUG("part_number = %u, total_parts = %u, image_count = %u",
153 hdr->part_number, hdr->total_parts, hdr->image_count);
155 if (hdr->image_count >= INT_MAX) {
156 ERROR("\"%"TS"\": Invalid image count (%u)",
157 filename, hdr->image_count);
158 return WIMLIB_ERR_IMAGE_COUNT;
161 get_resource_entry(&disk_hdr.lookup_table_res_entry, &hdr->lookup_table_res_entry);
162 get_resource_entry(&disk_hdr.xml_data_res_entry, &hdr->xml_res_entry);
163 get_resource_entry(&disk_hdr.boot_metadata_res_entry, &hdr->boot_metadata_res_entry);
164 hdr->boot_idx = le32_to_cpu(disk_hdr.boot_idx);
165 get_resource_entry(&disk_hdr.integrity_table_res_entry, &hdr->integrity);
169 ERROR_WITH_ERRNO("\"%"TS"\": Error reading header", filename);
173 /* Writes the header for a WIM file at the specified offset. If the offset
174 * specified is the current one, the position is advanced by the size of the
177 write_wim_header_at_offset(const struct wim_header *hdr, struct filedes *out_fd,
180 struct wim_header_disk disk_hdr _aligned_attribute(8);
183 DEBUG("Writing %sWIM header at offset %"PRIu64,
184 ((hdr->magic == PWM_MAGIC) ? "pipable " : ""),
187 disk_hdr.magic = hdr->magic;
188 disk_hdr.hdr_size = cpu_to_le32(sizeof(struct wim_header_disk));
189 disk_hdr.wim_version = cpu_to_le32(WIM_VERSION);
190 disk_hdr.wim_flags = cpu_to_le32(hdr->flags);
191 disk_hdr.chunk_size = cpu_to_le32((hdr->flags & WIM_HDR_FLAG_COMPRESSION) ?
193 memcpy(disk_hdr.guid, hdr->guid, WIM_GID_LEN);
195 disk_hdr.part_number = cpu_to_le16(hdr->part_number);
196 disk_hdr.total_parts = cpu_to_le16(hdr->total_parts);
197 disk_hdr.image_count = cpu_to_le32(hdr->image_count);
198 put_resource_entry(&hdr->lookup_table_res_entry, &disk_hdr.lookup_table_res_entry);
199 put_resource_entry(&hdr->xml_res_entry, &disk_hdr.xml_data_res_entry);
200 put_resource_entry(&hdr->boot_metadata_res_entry, &disk_hdr.boot_metadata_res_entry);
201 disk_hdr.boot_idx = cpu_to_le32(hdr->boot_idx);
202 put_resource_entry(&hdr->integrity, &disk_hdr.integrity_table_res_entry);
203 memset(disk_hdr.unused, 0, sizeof(disk_hdr.unused));
205 if (offset == out_fd->offset)
206 ret = full_write(out_fd, &disk_hdr, sizeof(disk_hdr));
208 ret = full_pwrite(out_fd, &disk_hdr, sizeof(disk_hdr), offset);
210 ERROR_WITH_ERRNO("Failed to write WIM header");
214 /* Writes the header for a WIM file at the output file descriptor's current
217 write_wim_header(const struct wim_header *hdr, struct filedes *out_fd)
219 return write_wim_header_at_offset(hdr, out_fd, out_fd->offset);
222 /* Update just the wim_flags field. */
224 write_wim_header_flags(u32 hdr_flags, struct filedes *out_fd)
226 le32 flags = cpu_to_le32(hdr_flags);
228 return full_pwrite(out_fd, &flags, sizeof(flags),
229 offsetof(struct wim_header_disk, wim_flags));
233 get_wim_hdr_cflags(int ctype)
236 case WIMLIB_COMPRESSION_TYPE_NONE:
238 case WIMLIB_COMPRESSION_TYPE_LZX:
239 return WIM_HDR_FLAG_COMPRESSION | WIM_HDR_FLAG_COMPRESS_LZX;
240 case WIMLIB_COMPRESSION_TYPE_XPRESS:
241 return WIM_HDR_FLAG_COMPRESSION | WIM_HDR_FLAG_COMPRESS_XPRESS;
242 case WIMLIB_COMPRESSION_TYPE_INVALID:
249 * Initializes the header for a WIM file.
252 init_wim_header(struct wim_header *hdr, int ctype)
254 memset(hdr, 0, sizeof(struct wim_header));
255 hdr->flags = get_wim_hdr_cflags(ctype);
256 if (hdr->flags == (u32)~0U) {
257 ERROR("Invalid compression type specified (%d)", ctype);
258 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
260 hdr->total_parts = 1;
261 hdr->part_number = 1;
262 randomize_byte_array(hdr->guid, sizeof(hdr->guid));
263 hdr->magic = WIM_MAGIC;
271 struct hdr_flag hdr_flags[] = {
272 {WIM_HDR_FLAG_RESERVED, "RESERVED"},
273 {WIM_HDR_FLAG_COMPRESSION, "COMPRESSION"},
274 {WIM_HDR_FLAG_READONLY, "READONLY"},
275 {WIM_HDR_FLAG_SPANNED, "SPANNED"},
276 {WIM_HDR_FLAG_RESOURCE_ONLY, "RESOURCE_ONLY"},
277 {WIM_HDR_FLAG_METADATA_ONLY, "METADATA_ONLY"},
278 {WIM_HDR_FLAG_WRITE_IN_PROGRESS,"WRITE_IN_PROGRESS"},
279 {WIM_HDR_FLAG_RP_FIX, "RP_FIX"},
280 {WIM_HDR_FLAG_COMPRESS_RESERVED,"COMPRESS_RESERVED"},
281 {WIM_HDR_FLAG_COMPRESS_LZX, "COMPRESS_LZX"},
282 {WIM_HDR_FLAG_COMPRESS_XPRESS, "COMPRESS_XPRESS"},
285 /* API function documented in wimlib.h */
287 wimlib_print_header(const WIMStruct *wim)
289 const struct wim_header *hdr = &wim->hdr;
291 tprintf(T("Magic Characters = MSWIM\\000\\000\\000\n"));
292 tprintf(T("Header Size = %u\n"), WIM_HEADER_DISK_SIZE);
293 tprintf(T("Version = 0x%x\n"), WIM_VERSION);
295 tprintf(T("Flags = 0x%x\n"), hdr->flags);
296 for (size_t i = 0; i < ARRAY_LEN(hdr_flags); i++)
297 if (hdr_flags[i].flag & hdr->flags)
298 tprintf(T(" WIM_HDR_FLAG_%s is set\n"), hdr_flags[i].name);
300 tprintf(T("Chunk Size = %u\n"), WIM_CHUNK_SIZE);
301 tfputs (T("GUID = "), stdout);
302 print_byte_field(hdr->guid, WIM_GID_LEN, stdout);
304 tprintf(T("Part Number = %hu\n"), wim->hdr.part_number);
305 tprintf(T("Total Parts = %hu\n"), wim->hdr.total_parts);
306 tprintf(T("Image Count = %u\n"), hdr->image_count);
307 tprintf(T("Lookup Table Size = %"PRIu64"\n"),
308 (u64)hdr->lookup_table_res_entry.size);
309 tprintf(T("Lookup Table Flags = 0x%hhx\n"),
310 (u8)hdr->lookup_table_res_entry.flags);
311 tprintf(T("Lookup Table Offset = %"PRIu64"\n"),
312 hdr->lookup_table_res_entry.offset);
313 tprintf(T("Lookup Table Original_size = %"PRIu64"\n"),
314 hdr->lookup_table_res_entry.original_size);
315 tprintf(T("XML Data Size = %"PRIu64"\n"),
316 (u64)hdr->xml_res_entry.size);
317 tprintf(T("XML Data Flags = 0x%hhx\n"),
318 (u8)hdr->xml_res_entry.flags);
319 tprintf(T("XML Data Offset = %"PRIu64"\n"),
320 hdr->xml_res_entry.offset);
321 tprintf(T("XML Data Original Size = %"PRIu64"\n"),
322 hdr->xml_res_entry.original_size);
323 tprintf(T("Boot Metadata Size = %"PRIu64"\n"),
324 (u64)hdr->boot_metadata_res_entry.size);
325 tprintf(T("Boot Metadata Flags = 0x%hhx\n"),
326 (u8)hdr->boot_metadata_res_entry.flags);
327 tprintf(T("Boot Metadata Offset = %"PRIu64"\n"),
328 hdr->boot_metadata_res_entry.offset);
329 tprintf(T("Boot Metadata Original Size = %"PRIu64"\n"),
330 hdr->boot_metadata_res_entry.original_size);
331 tprintf(T("Boot Index = %u\n"), hdr->boot_idx);
332 tprintf(T("Integrity Size = %"PRIu64"\n"),
333 (u64)hdr->integrity.size);
334 tprintf(T("Integrity Flags = 0x%hhx\n"),
335 (u8)hdr->integrity.flags);
336 tprintf(T("Integrity Offset = %"PRIu64"\n"),
337 hdr->integrity.offset);
338 tprintf(T("Integrity Original_size = %"PRIu64"\n"),
339 hdr->integrity.original_size);