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/buffer_io.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"
41 /* First 8 bytes in every WIM file. */
42 static const u8 wim_magic_chars[WIM_MAGIC_LEN] = {
43 'M', 'S', 'W', 'I', 'M', '\0', '\0', '\0' };
45 /* Reads the header from a WIM file. */
47 read_header(const tchar *filename, int in_fd,
48 struct wim_header *hdr, int open_flags)
51 u8 buf[WIM_HEADER_DISK_SIZE];
58 DEBUG("Reading WIM header from \"%"TS"\".", filename);
60 bytes_read = full_pread(in_fd, buf, WIM_HEADER_DISK_SIZE, 0);
62 if (bytes_read != WIM_HEADER_DISK_SIZE) {
63 ERROR_WITH_ERRNO("\"%"TS"\": Error reading header", filename);
64 return WIMLIB_ERR_READ;
68 if (memcmp(p, wim_magic_chars, WIM_MAGIC_LEN)) {
69 ERROR("\"%"TS"\": Invalid magic characters in header", filename);
70 return WIMLIB_ERR_NOT_A_WIM_FILE;
74 p = get_u32(p + 8, &hdr_size);
75 if (hdr_size != WIM_HEADER_DISK_SIZE) {
76 ERROR("\"%"TS"\": Header is %u bytes (expected %u bytes)",
77 filename, hdr_size, WIM_HEADER_DISK_SIZE);
78 return WIMLIB_ERR_INVALID_HEADER_SIZE;
82 p = get_u32(buf + WIM_MAGIC_LEN + sizeof(u32), &wim_version);
83 if (wim_version != WIM_VERSION) {
84 ERROR("\"%"TS"\": The WIM header says the WIM version is %u, "
85 "but wimlib only knows about version %u",
86 filename, wim_version, WIM_VERSION);
87 return WIMLIB_ERR_UNKNOWN_VERSION;
90 p = get_u32(p, &hdr->flags);
91 p = get_u32(p, &chunk_size);
92 if (chunk_size != WIM_CHUNK_SIZE &&
93 (hdr->flags & WIM_HDR_FLAG_COMPRESSION)) {
94 ERROR("\"%"TS"\": Unexpected chunk size of %u! Ask the author to "
95 "implement support for other chunk sizes.",
96 filename, chunk_size);
97 ERROR("(Or it might just be that the WIM header is invalid.)");
98 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
101 p = get_bytes(p, WIM_GID_LEN, hdr->guid);
102 p = get_u16(p, &hdr->part_number);
103 p = get_u16(p, &hdr->total_parts);
105 if (hdr->total_parts == 0 ||
106 hdr->part_number == 0 ||
107 hdr->part_number > hdr->total_parts)
109 ERROR("\"%"TS"\": Invalid WIM part number: %hu of %hu",
110 filename, hdr->part_number, hdr->total_parts);
111 return WIMLIB_ERR_INVALID_PART_NUMBER;
114 if (!(open_flags & WIMLIB_OPEN_FLAG_SPLIT_OK) &&
115 hdr->total_parts != 1)
117 ERROR("\"%"TS"\": This WIM is part %u of a %u-part WIM",
118 filename, hdr->part_number, hdr->total_parts);
119 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
122 p = get_u32(p, &hdr->image_count);
124 DEBUG("part_number = %u, total_parts = %u, image_count = %u",
125 hdr->part_number, hdr->total_parts, hdr->image_count);
127 if (hdr->image_count >= INT_MAX) {
128 ERROR("\"%"TS"\": Invalid image count (%u)",
129 filename, hdr->image_count);
130 return WIMLIB_ERR_IMAGE_COUNT;
135 p = get_resource_entry(p, &hdr->lookup_table_res_entry);
136 p = get_resource_entry(p, &hdr->xml_res_entry);
137 p = get_resource_entry(p, &hdr->boot_metadata_res_entry);
141 p = get_u32(p, &hdr->boot_idx);
145 p = get_resource_entry(p, &hdr->integrity);
149 /* 60 bytes of unused stuff. */
157 * Writes the header for a WIM file.
159 * @hdr: A pointer to a struct wim_header structure that describes the header.
160 * @out_fd: The file descriptor to the WIM file, opened for writing.
162 * Returns zero on success, nonzero on failure.
165 write_header(const struct wim_header *hdr, int out_fd)
167 u8 buf[WIM_HEADER_DISK_SIZE];
169 DEBUG("Writing WIM header.");
171 p = put_bytes(buf, WIM_MAGIC_LEN, wim_magic_chars);
172 p = put_u32(p, WIM_HEADER_DISK_SIZE);
173 p = put_u32(p, WIM_VERSION);
174 p = put_u32(p, hdr->flags);
175 p = put_u32(p, (hdr->flags & WIM_HDR_FLAG_COMPRESSION) ?
179 p = put_bytes(p, WIM_GID_LEN, hdr->guid);
180 p = put_u16(p, hdr->part_number);
184 p = put_u16(p, hdr->total_parts);
185 p = put_u32(p, hdr->image_count);
186 p = put_resource_entry(p, &hdr->lookup_table_res_entry);
187 p = put_resource_entry(p, &hdr->xml_res_entry);
188 p = put_resource_entry(p, &hdr->boot_metadata_res_entry);
189 p = put_u32(p, hdr->boot_idx);
190 p = put_resource_entry(p, &hdr->integrity);
191 p = put_zeroes(p, WIM_UNUSED_LEN);
192 wimlib_assert(p - buf == sizeof(buf));
194 if (full_pwrite(out_fd, buf, sizeof(buf), 0) != sizeof(buf)) {
195 ERROR_WITH_ERRNO("Failed to write WIM header");
196 return WIMLIB_ERR_WRITE;
198 DEBUG("Done writing WIM header");
203 * Initializes the header for a WIM file.
206 init_header(struct wim_header *hdr, int ctype)
208 memset(hdr, 0, sizeof(struct wim_header));
210 case WIMLIB_COMPRESSION_TYPE_NONE:
213 case WIMLIB_COMPRESSION_TYPE_LZX:
214 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
215 WIM_HDR_FLAG_COMPRESS_LZX;
217 case WIMLIB_COMPRESSION_TYPE_XPRESS:
218 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
219 WIM_HDR_FLAG_COMPRESS_XPRESS;
222 ERROR("Invalid compression type specified (%d)", ctype);
223 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
225 hdr->total_parts = 1;
226 hdr->part_number = 1;
227 randomize_byte_array(hdr->guid, sizeof(hdr->guid));
235 struct hdr_flag hdr_flags[] = {
236 {WIM_HDR_FLAG_RESERVED, "RESERVED"},
237 {WIM_HDR_FLAG_COMPRESSION, "COMPRESSION"},
238 {WIM_HDR_FLAG_READONLY, "READONLY"},
239 {WIM_HDR_FLAG_SPANNED, "SPANNED"},
240 {WIM_HDR_FLAG_RESOURCE_ONLY, "RESOURCE_ONLY"},
241 {WIM_HDR_FLAG_METADATA_ONLY, "METADATA_ONLY"},
242 {WIM_HDR_FLAG_WRITE_IN_PROGRESS,"WRITE_IN_PROGRESS"},
243 {WIM_HDR_FLAG_RP_FIX, "RP_FIX"},
244 {WIM_HDR_FLAG_COMPRESS_RESERVED,"COMPRESS_RESERVED"},
245 {WIM_HDR_FLAG_COMPRESS_LZX, "COMPRESS_LZX"},
246 {WIM_HDR_FLAG_COMPRESS_XPRESS, "COMPRESS_XPRESS"},
249 /* Prints information from the header of the WIM file associated with @w. */
251 wimlib_print_header(const WIMStruct *w)
253 const struct wim_header *hdr = &w->hdr;
255 tprintf(T("Magic Characters = MSWIM\\000\\000\\000\n"));
256 tprintf(T("Header Size = %u\n"), WIM_HEADER_DISK_SIZE);
257 tprintf(T("Version = 0x%x\n"), WIM_VERSION);
259 tprintf(T("Flags = 0x%x\n"), hdr->flags);
260 for (size_t i = 0; i < ARRAY_LEN(hdr_flags); i++)
261 if (hdr_flags[i].flag & hdr->flags)
262 tprintf(T(" WIM_HDR_FLAG_%s is set\n"), hdr_flags[i].name);
264 tprintf(T("Chunk Size = %u\n"), WIM_CHUNK_SIZE);
265 tfputs (T("GUID = "), stdout);
266 print_byte_field(hdr->guid, WIM_GID_LEN, stdout);
268 tprintf(T("Part Number = %hu\n"), w->hdr.part_number);
269 tprintf(T("Total Parts = %hu\n"), w->hdr.total_parts);
270 tprintf(T("Image Count = %u\n"), hdr->image_count);
271 tprintf(T("Lookup Table Size = %"PRIu64"\n"),
272 (u64)hdr->lookup_table_res_entry.size);
273 tprintf(T("Lookup Table Flags = 0x%hhx\n"),
274 (u8)hdr->lookup_table_res_entry.flags);
275 tprintf(T("Lookup Table Offset = %"PRIu64"\n"),
276 hdr->lookup_table_res_entry.offset);
277 tprintf(T("Lookup Table Original_size = %"PRIu64"\n"),
278 hdr->lookup_table_res_entry.original_size);
279 tprintf(T("XML Data Size = %"PRIu64"\n"),
280 (u64)hdr->xml_res_entry.size);
281 tprintf(T("XML Data Flags = 0x%hhx\n"),
282 (u8)hdr->xml_res_entry.flags);
283 tprintf(T("XML Data Offset = %"PRIu64"\n"),
284 hdr->xml_res_entry.offset);
285 tprintf(T("XML Data Original Size = %"PRIu64"\n"),
286 hdr->xml_res_entry.original_size);
287 tprintf(T("Boot Metadata Size = %"PRIu64"\n"),
288 (u64)hdr->boot_metadata_res_entry.size);
289 tprintf(T("Boot Metadata Flags = 0x%hhx\n"),
290 (u8)hdr->boot_metadata_res_entry.flags);
291 tprintf(T("Boot Metadata Offset = %"PRIu64"\n"),
292 hdr->boot_metadata_res_entry.offset);
293 tprintf(T("Boot Metadata Original Size = %"PRIu64"\n"),
294 hdr->boot_metadata_res_entry.original_size);
295 tprintf(T("Boot Index = %u\n"), hdr->boot_idx);
296 tprintf(T("Integrity Size = %"PRIu64"\n"),
297 (u64)hdr->integrity.size);
298 tprintf(T("Integrity Flags = 0x%hhx\n"),
299 (u8)hdr->integrity.flags);
300 tprintf(T("Integrity Offset = %"PRIu64"\n"),
301 hdr->integrity.offset);
302 tprintf(T("Integrity Original_size = %"PRIu64"\n"),
303 hdr->integrity.original_size);