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/.
26 #include "wimlib_internal.h"
27 #include "buffer_io.h"
30 /* First 8 bytes in every WIM file. */
31 static const u8 wim_magic_chars[WIM_MAGIC_LEN] = {
32 'M', 'S', 'W', 'I', 'M', '\0', '\0', '\0' };
34 /* Reads the header from a WIM file. */
36 read_header(const tchar *filename, int in_fd,
37 struct wim_header *hdr, int open_flags)
40 u8 buf[WIM_HEADER_DISK_SIZE];
47 DEBUG("Reading WIM header from \"%"TS"\".", filename);
49 bytes_read = full_pread(in_fd, buf, WIM_HEADER_DISK_SIZE, 0);
51 if (bytes_read != WIM_HEADER_DISK_SIZE) {
52 ERROR_WITH_ERRNO("\"%"TS"\": Error reading header", filename);
53 return WIMLIB_ERR_READ;
57 if (memcmp(p, wim_magic_chars, WIM_MAGIC_LEN)) {
58 ERROR("\"%"TS"\": Invalid magic characters in header", filename);
59 return WIMLIB_ERR_NOT_A_WIM_FILE;
63 p = get_u32(p + 8, &hdr_size);
64 if (hdr_size != WIM_HEADER_DISK_SIZE) {
65 ERROR("\"%"TS"\": Header is %u bytes (expected %u bytes)",
66 filename, hdr_size, WIM_HEADER_DISK_SIZE);
67 return WIMLIB_ERR_INVALID_HEADER_SIZE;
71 p = get_u32(buf + WIM_MAGIC_LEN + sizeof(u32), &wim_version);
72 if (wim_version != WIM_VERSION) {
73 ERROR("\"%"TS"\": The WIM header says the WIM version is %u, "
74 "but wimlib only knows about version %u",
75 filename, wim_version, WIM_VERSION);
76 return WIMLIB_ERR_UNKNOWN_VERSION;
79 p = get_u32(p, &hdr->flags);
80 p = get_u32(p, &chunk_size);
81 if (chunk_size != WIM_CHUNK_SIZE &&
82 (hdr->flags & WIM_HDR_FLAG_COMPRESSION)) {
83 ERROR("\"%"TS"\": Unexpected chunk size of %u! Ask the author to "
84 "implement support for other chunk sizes.",
85 filename, chunk_size);
86 ERROR("(Or it might just be that the WIM header is invalid.)");
87 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
90 p = get_bytes(p, WIM_GID_LEN, hdr->guid);
91 p = get_u16(p, &hdr->part_number);
92 p = get_u16(p, &hdr->total_parts);
94 if (hdr->total_parts == 0 ||
95 hdr->part_number == 0 ||
96 hdr->part_number > hdr->total_parts)
98 ERROR("\"%"TS"\": Invalid WIM part number: %hu of %hu",
99 filename, hdr->part_number, hdr->total_parts);
100 return WIMLIB_ERR_INVALID_PART_NUMBER;
103 if (!(open_flags & WIMLIB_OPEN_FLAG_SPLIT_OK) &&
104 hdr->total_parts != 1)
106 ERROR("\"%"TS"\": This WIM is part %u of a %u-part WIM",
107 filename, hdr->part_number, hdr->total_parts);
108 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
111 p = get_u32(p, &hdr->image_count);
113 DEBUG("part_number = %u, total_parts = %u, image_count = %u",
114 hdr->part_number, hdr->total_parts, hdr->image_count);
116 if (hdr->image_count >= INT_MAX) {
117 ERROR("\"%"TS"\": Invalid image count (%u)",
118 filename, hdr->image_count);
119 return WIMLIB_ERR_IMAGE_COUNT;
124 p = get_resource_entry(p, &hdr->lookup_table_res_entry);
125 p = get_resource_entry(p, &hdr->xml_res_entry);
126 p = get_resource_entry(p, &hdr->boot_metadata_res_entry);
130 p = get_u32(p, &hdr->boot_idx);
134 p = get_resource_entry(p, &hdr->integrity);
138 /* 60 bytes of unused stuff. */
146 * Writes the header for a WIM file.
148 * @hdr: A pointer to a struct wim_header structure that describes the header.
149 * @out_fd: The file descriptor to the WIM file, opened for writing.
151 * Returns zero on success, nonzero on failure.
154 write_header(const struct wim_header *hdr, int out_fd)
156 u8 buf[WIM_HEADER_DISK_SIZE];
158 DEBUG("Writing WIM header.");
160 p = put_bytes(buf, WIM_MAGIC_LEN, wim_magic_chars);
161 p = put_u32(p, WIM_HEADER_DISK_SIZE);
162 p = put_u32(p, WIM_VERSION);
163 p = put_u32(p, hdr->flags);
164 p = put_u32(p, (hdr->flags & WIM_HDR_FLAG_COMPRESSION) ?
168 p = put_bytes(p, WIM_GID_LEN, hdr->guid);
169 p = put_u16(p, hdr->part_number);
173 p = put_u16(p, hdr->total_parts);
174 p = put_u32(p, hdr->image_count);
175 p = put_resource_entry(p, &hdr->lookup_table_res_entry);
176 p = put_resource_entry(p, &hdr->xml_res_entry);
177 p = put_resource_entry(p, &hdr->boot_metadata_res_entry);
178 p = put_u32(p, hdr->boot_idx);
179 p = put_resource_entry(p, &hdr->integrity);
180 p = put_zeroes(p, WIM_UNUSED_LEN);
181 wimlib_assert(p - buf == sizeof(buf));
183 if (full_pwrite(out_fd, buf, sizeof(buf), 0) != sizeof(buf)) {
184 ERROR_WITH_ERRNO("Failed to write WIM header");
185 return WIMLIB_ERR_WRITE;
187 DEBUG("Done writing WIM header");
192 * Initializes the header for a WIM file.
195 init_header(struct wim_header *hdr, int ctype)
197 memset(hdr, 0, sizeof(struct wim_header));
199 case WIMLIB_COMPRESSION_TYPE_NONE:
202 case WIMLIB_COMPRESSION_TYPE_LZX:
203 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
204 WIM_HDR_FLAG_COMPRESS_LZX;
206 case WIMLIB_COMPRESSION_TYPE_XPRESS:
207 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
208 WIM_HDR_FLAG_COMPRESS_XPRESS;
211 ERROR("Invalid compression type specified (%d)", ctype);
212 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
214 hdr->total_parts = 1;
215 hdr->part_number = 1;
216 randomize_byte_array(hdr->guid, sizeof(hdr->guid));
224 struct hdr_flag hdr_flags[] = {
225 {WIM_HDR_FLAG_RESERVED, "RESERVED"},
226 {WIM_HDR_FLAG_COMPRESSION, "COMPRESSION"},
227 {WIM_HDR_FLAG_READONLY, "READONLY"},
228 {WIM_HDR_FLAG_SPANNED, "SPANNED"},
229 {WIM_HDR_FLAG_RESOURCE_ONLY, "RESOURCE_ONLY"},
230 {WIM_HDR_FLAG_METADATA_ONLY, "METADATA_ONLY"},
231 {WIM_HDR_FLAG_WRITE_IN_PROGRESS,"WRITE_IN_PROGRESS"},
232 {WIM_HDR_FLAG_RP_FIX, "RP_FIX"},
233 {WIM_HDR_FLAG_COMPRESS_RESERVED,"COMPRESS_RESERVED"},
234 {WIM_HDR_FLAG_COMPRESS_LZX, "COMPRESS_LZX"},
235 {WIM_HDR_FLAG_COMPRESS_XPRESS, "COMPRESS_XPRESS"},
238 /* Prints information from the header of the WIM file associated with @w. */
240 wimlib_print_header(const WIMStruct *w)
242 const struct wim_header *hdr = &w->hdr;
244 tprintf(T("Magic Characters = MSWIM\\000\\000\\000\n"));
245 tprintf(T("Header Size = %u\n"), WIM_HEADER_DISK_SIZE);
246 tprintf(T("Version = 0x%x\n"), WIM_VERSION);
248 tprintf(T("Flags = 0x%x\n"), hdr->flags);
249 for (size_t i = 0; i < ARRAY_LEN(hdr_flags); i++)
250 if (hdr_flags[i].flag & hdr->flags)
251 tprintf(T(" WIM_HDR_FLAG_%s is set\n"), hdr_flags[i].name);
253 tprintf(T("Chunk Size = %u\n"), WIM_CHUNK_SIZE);
254 tfputs (T("GUID = "), stdout);
255 print_byte_field(hdr->guid, WIM_GID_LEN, stdout);
257 tprintf(T("Part Number = %hu\n"), w->hdr.part_number);
258 tprintf(T("Total Parts = %hu\n"), w->hdr.total_parts);
259 tprintf(T("Image Count = %u\n"), hdr->image_count);
260 tprintf(T("Lookup Table Size = %"PRIu64"\n"),
261 (u64)hdr->lookup_table_res_entry.size);
262 tprintf(T("Lookup Table Flags = 0x%hhx\n"),
263 (u8)hdr->lookup_table_res_entry.flags);
264 tprintf(T("Lookup Table Offset = %"PRIu64"\n"),
265 hdr->lookup_table_res_entry.offset);
266 tprintf(T("Lookup Table Original_size = %"PRIu64"\n"),
267 hdr->lookup_table_res_entry.original_size);
268 tprintf(T("XML Data Size = %"PRIu64"\n"),
269 (u64)hdr->xml_res_entry.size);
270 tprintf(T("XML Data Flags = 0x%hhx\n"),
271 (u8)hdr->xml_res_entry.flags);
272 tprintf(T("XML Data Offset = %"PRIu64"\n"),
273 hdr->xml_res_entry.offset);
274 tprintf(T("XML Data Original Size = %"PRIu64"\n"),
275 hdr->xml_res_entry.original_size);
276 tprintf(T("Boot Metadata Size = %"PRIu64"\n"),
277 (u64)hdr->boot_metadata_res_entry.size);
278 tprintf(T("Boot Metadata Flags = 0x%hhx\n"),
279 (u8)hdr->boot_metadata_res_entry.flags);
280 tprintf(T("Boot Metadata Offset = %"PRIu64"\n"),
281 hdr->boot_metadata_res_entry.offset);
282 tprintf(T("Boot Metadata Original Size = %"PRIu64"\n"),
283 hdr->boot_metadata_res_entry.original_size);
284 tprintf(T("Boot Index = %u\n"), hdr->boot_idx);
285 tprintf(T("Integrity Size = %"PRIu64"\n"),
286 (u64)hdr->integrity.size);
287 tprintf(T("Integrity Flags = 0x%hhx\n"),
288 (u8)hdr->integrity.flags);
289 tprintf(T("Integrity Offset = %"PRIu64"\n"),
290 hdr->integrity.offset);
291 tprintf(T("Integrity Original_size = %"PRIu64"\n"),
292 hdr->integrity.original_size);