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 * WIM to read the header from. @wim->in_fd must be positioned at the
53 * beginning of the file.
56 * Structure to read the header into.
59 * WIMLIB_ERR_SUCCESS (0)
60 * WIMLIB_ERR_IMAGE_COUNT
61 * WIMLIB_ERR_INVALID_PART_NUMBER
62 * WIMLIB_ERR_NOT_A_WIM_FILE
64 * WIMLIB_ERR_UNEXPECTED_END_OF_FILE
65 * WIMLIB_ERR_UNKNOWN_VERSION
68 read_wim_header(WIMStruct *wim, struct wim_header *hdr)
70 struct wim_header_disk disk_hdr _aligned_attribute(8);
71 struct filedes *in_fd = &wim->in_fd;
72 const tchar *filename = wim->filename;
76 wimlib_assert(in_fd->offset == 0);
78 if (filename == NULL) {
79 pipe_str = alloca(40);
80 tsprintf(pipe_str, T("[fd %d]"), in_fd->fd);
84 BUILD_BUG_ON(sizeof(struct wim_header_disk) != WIM_HEADER_DISK_SIZE);
86 DEBUG("Reading WIM header from \"%"TS"\"", filename);
88 ret = full_read(in_fd, &disk_hdr, sizeof(disk_hdr));
92 hdr->magic = le64_to_cpu(disk_hdr.magic);
94 if (hdr->magic != WIM_MAGIC) {
95 if (hdr->magic == PWM_MAGIC) {
96 /* Pipable WIM: Use header at end instead, unless
97 * actually reading from a pipe. */
98 if (!in_fd->is_pipe) {
99 ret = WIMLIB_ERR_READ;
100 if (-1 == lseek(in_fd->fd, -WIM_HEADER_DISK_SIZE, SEEK_END))
102 ret = full_read(in_fd, &disk_hdr, sizeof(disk_hdr));
107 ERROR("\"%"TS"\": Invalid magic characters in header", filename);
108 return WIMLIB_ERR_NOT_A_WIM_FILE;
112 if (le32_to_cpu(disk_hdr.hdr_size) != sizeof(struct wim_header_disk)) {
113 ERROR("\"%"TS"\": Header size is invalid (%u bytes)",
114 filename, le32_to_cpu(disk_hdr.hdr_size));
115 return WIMLIB_ERR_INVALID_HEADER;
118 hdr->wim_version = le32_to_cpu(disk_hdr.wim_version);
119 if (hdr->wim_version != WIM_VERSION_DEFAULT &&
120 hdr->wim_version != WIM_VERSION_PACKED_STREAMS)
122 ERROR("\"%"TS"\": Unknown WIM version: %u",
123 filename, hdr->wim_version);
124 return WIMLIB_ERR_UNKNOWN_VERSION;
127 hdr->flags = le32_to_cpu(disk_hdr.wim_flags);
128 hdr->chunk_size = le32_to_cpu(disk_hdr.chunk_size);
129 memcpy(hdr->guid, disk_hdr.guid, WIM_GUID_LEN);
130 hdr->part_number = le16_to_cpu(disk_hdr.part_number);
131 hdr->total_parts = le16_to_cpu(disk_hdr.total_parts);
133 if (hdr->total_parts == 0 || hdr->part_number == 0 ||
134 hdr->part_number > hdr->total_parts)
136 ERROR("\"%"TS"\": Invalid WIM part number: %hu of %hu",
137 filename, hdr->part_number, hdr->total_parts);
138 return WIMLIB_ERR_INVALID_PART_NUMBER;
141 hdr->image_count = le32_to_cpu(disk_hdr.image_count);
143 DEBUG("part_number = %u, total_parts = %u, image_count = %u",
144 hdr->part_number, hdr->total_parts, hdr->image_count);
146 if (unlikely(hdr->image_count > MAX_IMAGES)) {
147 ERROR("\"%"TS"\": Invalid image count (%u)",
148 filename, hdr->image_count);
149 return WIMLIB_ERR_IMAGE_COUNT;
152 get_wim_reshdr(&disk_hdr.lookup_table_reshdr, &hdr->lookup_table_reshdr);
153 get_wim_reshdr(&disk_hdr.xml_data_reshdr, &hdr->xml_data_reshdr);
154 get_wim_reshdr(&disk_hdr.boot_metadata_reshdr, &hdr->boot_metadata_reshdr);
155 hdr->boot_idx = le32_to_cpu(disk_hdr.boot_idx);
156 get_wim_reshdr(&disk_hdr.integrity_table_reshdr, &hdr->integrity_table_reshdr);
160 ERROR_WITH_ERRNO("\"%"TS"\": Error reading header", filename);
164 /* Writes the header for a WIM file at the specified offset. If the offset
165 * specified is the current one, the position is advanced by the size of the
168 write_wim_header_at_offset(const struct wim_header *hdr, struct filedes *out_fd,
171 struct wim_header_disk disk_hdr _aligned_attribute(8);
174 DEBUG("Writing %sWIM header at offset %"PRIu64,
175 ((hdr->magic == PWM_MAGIC) ? "pipable " : ""),
178 disk_hdr.magic = cpu_to_le64(hdr->magic);
179 disk_hdr.hdr_size = cpu_to_le32(sizeof(struct wim_header_disk));
180 disk_hdr.wim_version = cpu_to_le32(hdr->wim_version);
181 disk_hdr.wim_flags = cpu_to_le32(hdr->flags);
182 if (hdr->flags & WIM_HDR_FLAG_COMPRESSION)
183 disk_hdr.chunk_size = cpu_to_le32(hdr->chunk_size);
185 disk_hdr.chunk_size = 0;
186 memcpy(disk_hdr.guid, hdr->guid, WIM_GUID_LEN);
188 disk_hdr.part_number = cpu_to_le16(hdr->part_number);
189 disk_hdr.total_parts = cpu_to_le16(hdr->total_parts);
190 disk_hdr.image_count = cpu_to_le32(hdr->image_count);
191 put_wim_reshdr(&hdr->lookup_table_reshdr, &disk_hdr.lookup_table_reshdr);
192 put_wim_reshdr(&hdr->xml_data_reshdr, &disk_hdr.xml_data_reshdr);
193 put_wim_reshdr(&hdr->boot_metadata_reshdr, &disk_hdr.boot_metadata_reshdr);
194 disk_hdr.boot_idx = cpu_to_le32(hdr->boot_idx);
195 put_wim_reshdr(&hdr->integrity_table_reshdr, &disk_hdr.integrity_table_reshdr);
196 memset(disk_hdr.unused, 0, sizeof(disk_hdr.unused));
198 if (offset == out_fd->offset)
199 ret = full_write(out_fd, &disk_hdr, sizeof(disk_hdr));
201 ret = full_pwrite(out_fd, &disk_hdr, sizeof(disk_hdr), offset);
203 ERROR_WITH_ERRNO("Failed to write WIM header");
207 /* Writes the header for a WIM file at the output file descriptor's current
210 write_wim_header(const struct wim_header *hdr, struct filedes *out_fd)
212 return write_wim_header_at_offset(hdr, out_fd, out_fd->offset);
215 /* Update just the wim_flags field. */
217 write_wim_header_flags(u32 hdr_flags, struct filedes *out_fd)
219 le32 flags = cpu_to_le32(hdr_flags);
221 return full_pwrite(out_fd, &flags, sizeof(flags),
222 offsetof(struct wim_header_disk, wim_flags));
226 set_wim_hdr_cflags(int ctype, struct wim_header *hdr)
228 hdr->flags &= ~(WIM_HDR_FLAG_COMPRESSION |
229 WIM_HDR_FLAG_COMPRESS_LZX |
230 WIM_HDR_FLAG_COMPRESS_RESERVED |
231 WIM_HDR_FLAG_COMPRESS_XPRESS |
232 WIM_HDR_FLAG_COMPRESS_LZMS |
233 WIM_HDR_FLAG_COMPRESS_XPRESS_2);
236 case WIMLIB_COMPRESSION_TYPE_NONE:
239 case WIMLIB_COMPRESSION_TYPE_LZX:
240 hdr->flags |= WIM_HDR_FLAG_COMPRESSION | WIM_HDR_FLAG_COMPRESS_LZX;
243 case WIMLIB_COMPRESSION_TYPE_XPRESS:
244 hdr->flags |= WIM_HDR_FLAG_COMPRESSION | WIM_HDR_FLAG_COMPRESS_XPRESS;
247 case WIMLIB_COMPRESSION_TYPE_LZMS:
248 hdr->flags |= WIM_HDR_FLAG_COMPRESSION | WIM_HDR_FLAG_COMPRESS_LZMS;
252 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
257 * Initializes the header for a WIM file.
260 init_wim_header(struct wim_header *hdr, int ctype, u32 chunk_size)
262 memset(hdr, 0, sizeof(struct wim_header));
263 hdr->magic = WIM_MAGIC;
265 if (ctype == WIMLIB_COMPRESSION_TYPE_LZMS)
266 hdr->wim_version = WIM_VERSION_PACKED_STREAMS;
268 hdr->wim_version = WIM_VERSION_DEFAULT;
269 if (set_wim_hdr_cflags(ctype, hdr)) {
270 ERROR("Invalid compression type specified (%d)", ctype);
271 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
273 hdr->chunk_size = chunk_size;
274 hdr->total_parts = 1;
275 hdr->part_number = 1;
276 randomize_byte_array(hdr->guid, sizeof(hdr->guid));
284 struct hdr_flag hdr_flags[] = {
285 {WIM_HDR_FLAG_RESERVED, "RESERVED"},
286 {WIM_HDR_FLAG_COMPRESSION, "COMPRESSION"},
287 {WIM_HDR_FLAG_READONLY, "READONLY"},
288 {WIM_HDR_FLAG_SPANNED, "SPANNED"},
289 {WIM_HDR_FLAG_RESOURCE_ONLY, "RESOURCE_ONLY"},
290 {WIM_HDR_FLAG_METADATA_ONLY, "METADATA_ONLY"},
291 {WIM_HDR_FLAG_WRITE_IN_PROGRESS,"WRITE_IN_PROGRESS"},
292 {WIM_HDR_FLAG_RP_FIX, "RP_FIX"},
293 {WIM_HDR_FLAG_COMPRESS_RESERVED,"COMPRESS_RESERVED"},
294 {WIM_HDR_FLAG_COMPRESS_LZX, "COMPRESS_LZX"},
295 {WIM_HDR_FLAG_COMPRESS_XPRESS, "COMPRESS_XPRESS"},
296 {WIM_HDR_FLAG_COMPRESS_LZMS, "COMPRESS_LZMS"},
297 {WIM_HDR_FLAG_COMPRESS_XPRESS_2,"COMPRESS_XPRESS_2"},
300 /* API function documented in wimlib.h */
302 wimlib_print_header(const WIMStruct *wim)
304 const struct wim_header *hdr = &wim->hdr;
306 tprintf(T("Magic Characters = MSWIM\\000\\000\\000\n"));
307 tprintf(T("Header Size = %u\n"), WIM_HEADER_DISK_SIZE);
308 tprintf(T("Version = 0x%x\n"), hdr->wim_version);
310 tprintf(T("Flags = 0x%x\n"), hdr->flags);
311 for (size_t i = 0; i < ARRAY_LEN(hdr_flags); i++)
312 if (hdr_flags[i].flag & hdr->flags)
313 tprintf(T(" WIM_HDR_FLAG_%s is set\n"), hdr_flags[i].name);
315 tprintf(T("Chunk Size = %u\n"), hdr->chunk_size);
316 tfputs (T("GUID = "), stdout);
317 print_byte_field(hdr->guid, WIM_GUID_LEN, stdout);
319 tprintf(T("Part Number = %hu\n"), hdr->part_number);
320 tprintf(T("Total Parts = %hu\n"), hdr->total_parts);
321 tprintf(T("Image Count = %u\n"), hdr->image_count);
322 tprintf(T("Lookup Table Size = %"PRIu64"\n"),
323 (u64)hdr->lookup_table_reshdr.size_in_wim);
324 tprintf(T("Lookup Table Flags = 0x%hhx\n"),
325 (u8)hdr->lookup_table_reshdr.flags);
326 tprintf(T("Lookup Table Offset = %"PRIu64"\n"),
327 hdr->lookup_table_reshdr.offset_in_wim);
328 tprintf(T("Lookup Table Original_size = %"PRIu64"\n"),
329 hdr->lookup_table_reshdr.uncompressed_size);
330 tprintf(T("XML Data Size = %"PRIu64"\n"),
331 (u64)hdr->xml_data_reshdr.size_in_wim);
332 tprintf(T("XML Data Flags = 0x%hhx\n"),
333 (u8)hdr->xml_data_reshdr.flags);
334 tprintf(T("XML Data Offset = %"PRIu64"\n"),
335 hdr->xml_data_reshdr.offset_in_wim);
336 tprintf(T("XML Data Original Size = %"PRIu64"\n"),
337 hdr->xml_data_reshdr.uncompressed_size);
338 tprintf(T("Boot Metadata Size = %"PRIu64"\n"),
339 (u64)hdr->boot_metadata_reshdr.size_in_wim);
340 tprintf(T("Boot Metadata Flags = 0x%hhx\n"),
341 (u8)hdr->boot_metadata_reshdr.flags);
342 tprintf(T("Boot Metadata Offset = %"PRIu64"\n"),
343 hdr->boot_metadata_reshdr.offset_in_wim);
344 tprintf(T("Boot Metadata Original Size = %"PRIu64"\n"),
345 hdr->boot_metadata_reshdr.uncompressed_size);
346 tprintf(T("Boot Index = %u\n"), hdr->boot_idx);
347 tprintf(T("Integrity Size = %"PRIu64"\n"),
348 (u64)hdr->integrity_table_reshdr.size_in_wim);
349 tprintf(T("Integrity Flags = 0x%hhx\n"),
350 (u8)hdr->integrity_table_reshdr.flags);
351 tprintf(T("Integrity Offset = %"PRIu64"\n"),
352 hdr->integrity_table_reshdr.offset_in_wim);
353 tprintf(T("Integrity Original_size = %"PRIu64"\n"),
354 hdr->integrity_table_reshdr.uncompressed_size);