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 for a WIM file. */
36 read_header(filedes_t in_fd, struct wim_header *hdr, int open_flags)
39 u8 buf[WIM_HEADER_DISK_SIZE];
46 DEBUG("Reading WIM header.");
48 bytes_read = full_read(in_fd, buf, WIM_HEADER_DISK_SIZE);
50 if (bytes_read != WIM_HEADER_DISK_SIZE) {
51 ERROR_WITH_ERRNO("Error reading WIM header");
52 return WIMLIB_ERR_READ;
59 if (memcmp(p, wim_magic_chars, WIM_MAGIC_LEN)) {
60 ERROR("Invalid magic characters in WIM header");
61 return WIMLIB_ERR_NOT_A_WIM_FILE;
65 p = get_u32(p, &hdr_size);
69 if (hdr_size != WIM_HEADER_DISK_SIZE) {
70 ERROR("Header is %u bytes (expected %u bytes)",
71 hdr_size, WIM_HEADER_DISK_SIZE);
72 return WIMLIB_ERR_INVALID_HEADER_SIZE;
75 p = get_u32(buf + WIM_MAGIC_LEN + sizeof(u32), &wim_version);
77 if (wim_version != WIM_VERSION) {
78 ERROR("The WIM header says the WIM version is %u, but wimlib "
79 "only knows about version %u",
80 wim_version, WIM_VERSION);
81 return WIMLIB_ERR_UNKNOWN_VERSION;
84 p = get_u32(p, &hdr->flags);
85 p = get_u32(p, &chunk_size);
86 if (chunk_size != WIM_CHUNK_SIZE &&
87 (hdr->flags & WIM_HDR_FLAG_COMPRESSION)) {
88 ERROR("Unexpected chunk size of %u! Ask the author to "
89 "implement support for other chunk sizes.",
91 ERROR("(Or it might just be that the WIM header is invalid.)");
92 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
95 p = get_bytes(p, WIM_GID_LEN, hdr->guid);
96 p = get_u16(p, &hdr->part_number);
97 p = get_u16(p, &hdr->total_parts);
99 if (hdr->total_parts == 0 ||
100 hdr->part_number == 0 ||
101 hdr->part_number > hdr->total_parts)
103 ERROR("Invalid WIM part number: %hu of %hu",
104 hdr->part_number, hdr->total_parts);
105 return WIMLIB_ERR_INVALID_PART_NUMBER;
108 if (!(open_flags & WIMLIB_OPEN_FLAG_SPLIT_OK) &&
109 hdr->total_parts != 1)
111 ERROR("This WIM is part %u of a %u-part WIM",
112 hdr->part_number, hdr->total_parts);
113 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
116 p = get_u32(p, &hdr->image_count);
118 DEBUG("part_number = %u, total_parts = %u, image_count = %u",
119 hdr->part_number, hdr->total_parts, hdr->image_count);
121 if (hdr->image_count >= INT_MAX) {
122 ERROR("Invalid image count (%u)", hdr->image_count);
123 return WIMLIB_ERR_IMAGE_COUNT;
128 p = get_resource_entry(p, &hdr->lookup_table_res_entry);
129 p = get_resource_entry(p, &hdr->xml_res_entry);
130 p = get_resource_entry(p, &hdr->boot_metadata_res_entry);
134 p = get_u32(p, &hdr->boot_idx);
138 p = get_resource_entry(p, &hdr->integrity);
142 /* 60 bytes of unused stuff. */
150 * Writes the header for a WIM file.
152 * @hdr: A pointer to a struct wim_header structure that describes the header.
153 * @out_fd: The file descriptor to the WIM file, opened for writing.
154 * @return: Zero on success, nonzero on failure.
157 write_header(const struct wim_header *hdr, int out_fd)
159 u8 buf[WIM_HEADER_DISK_SIZE];
161 DEBUG("Writing WIM header.");
163 p = put_bytes(buf, WIM_MAGIC_LEN, wim_magic_chars);
164 p = put_u32(p, WIM_HEADER_DISK_SIZE);
165 p = put_u32(p, WIM_VERSION);
166 p = put_u32(p, hdr->flags);
167 p = put_u32(p, (hdr->flags & WIM_HDR_FLAG_COMPRESSION) ?
171 p = put_bytes(p, WIM_GID_LEN, hdr->guid);
172 p = put_u16(p, hdr->part_number);
176 p = put_u16(p, hdr->total_parts);
177 p = put_u32(p, hdr->image_count);
178 p = put_resource_entry(p, &hdr->lookup_table_res_entry);
179 p = put_resource_entry(p, &hdr->xml_res_entry);
180 p = put_resource_entry(p, &hdr->boot_metadata_res_entry);
181 p = put_u32(p, hdr->boot_idx);
182 p = put_resource_entry(p, &hdr->integrity);
183 p = put_zeroes(p, WIM_UNUSED_LEN);
184 assert(p - buf == sizeof(buf));
186 if (full_pwrite(out_fd, buf, sizeof(buf), 0) != sizeof(buf)) {
187 ERROR_WITH_ERRNO("Failed to write WIM header");
188 return WIMLIB_ERR_WRITE;
190 DEBUG("Done writing WIM header");
195 * Initializes the header for a WIM file.
198 init_header(struct wim_header *hdr, int ctype)
200 memset(hdr, 0, sizeof(struct wim_header));
202 case WIMLIB_COMPRESSION_TYPE_NONE:
205 case WIMLIB_COMPRESSION_TYPE_LZX:
206 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
207 WIM_HDR_FLAG_COMPRESS_LZX;
209 case WIMLIB_COMPRESSION_TYPE_XPRESS:
210 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
211 WIM_HDR_FLAG_COMPRESS_XPRESS;
214 ERROR("Invalid compression type specified (%d)", ctype);
215 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
217 hdr->total_parts = 1;
218 hdr->part_number = 1;
219 randomize_byte_array(hdr->guid, sizeof(hdr->guid));
227 struct hdr_flag hdr_flags[] = {
228 {WIM_HDR_FLAG_RESERVED, "RESERVED"},
229 {WIM_HDR_FLAG_COMPRESSION, "COMPRESSION"},
230 {WIM_HDR_FLAG_READONLY, "READONLY"},
231 {WIM_HDR_FLAG_SPANNED, "SPANNED"},
232 {WIM_HDR_FLAG_RESOURCE_ONLY, "RESOURCE_ONLY"},
233 {WIM_HDR_FLAG_METADATA_ONLY, "METADATA_ONLY"},
234 {WIM_HDR_FLAG_WRITE_IN_PROGRESS,"WRITE_IN_PROGRESS"},
235 {WIM_HDR_FLAG_RP_FIX, "RP_FIX"},
236 {WIM_HDR_FLAG_COMPRESS_RESERVED,"COMPRESS_RESERVED"},
237 {WIM_HDR_FLAG_COMPRESS_LZX, "COMPRESS_LZX"},
238 {WIM_HDR_FLAG_COMPRESS_XPRESS, "COMPRESS_XPRESS"},
241 /* Prints information from the header of the WIM file associated with @w. */
243 wimlib_print_header(const WIMStruct *w)
245 const struct wim_header *hdr = &w->hdr;
247 tprintf(T("Magic Characters = MSWIM\\000\\000\\000\n"));
248 tprintf(T("Header Size = %u\n"), WIM_HEADER_DISK_SIZE);
249 tprintf(T("Version = 0x%x\n"), WIM_VERSION);
251 tprintf(T("Flags = 0x%x\n"), hdr->flags);
252 for (size_t i = 0; i < ARRAY_LEN(hdr_flags); i++)
253 if (hdr_flags[i].flag & hdr->flags)
254 tprintf(T(" WIM_HDR_FLAG_%s is set\n"), hdr_flags[i].name);
256 tprintf(T("Chunk Size = %u\n"), WIM_CHUNK_SIZE);
257 tfputs (T("GUID = "), stdout);
258 print_byte_field(hdr->guid, WIM_GID_LEN, stdout);
260 tprintf(T("Part Number = %hu\n"), w->hdr.part_number);
261 tprintf(T("Total Parts = %hu\n"), w->hdr.total_parts);
262 tprintf(T("Image Count = %u\n"), hdr->image_count);
263 tprintf(T("Lookup Table Size = %"PRIu64"\n"),
264 (u64)hdr->lookup_table_res_entry.size);
265 tprintf(T("Lookup Table Flags = 0x%hhx\n"),
266 (u8)hdr->lookup_table_res_entry.flags);
267 tprintf(T("Lookup Table Offset = %"PRIu64"\n"),
268 hdr->lookup_table_res_entry.offset);
269 tprintf(T("Lookup Table Original_size = %"PRIu64"\n"),
270 hdr->lookup_table_res_entry.original_size);
271 tprintf(T("XML Data Size = %"PRIu64"\n"),
272 (u64)hdr->xml_res_entry.size);
273 tprintf(T("XML Data Flags = 0x%hhx\n"),
274 (u8)hdr->xml_res_entry.flags);
275 tprintf(T("XML Data Offset = %"PRIu64"\n"),
276 hdr->xml_res_entry.offset);
277 tprintf(T("XML Data Original Size = %"PRIu64"\n"),
278 hdr->xml_res_entry.original_size);
279 tprintf(T("Boot Metadata Size = %"PRIu64"\n"),
280 (u64)hdr->boot_metadata_res_entry.size);
281 tprintf(T("Boot Metadata Flags = 0x%hhx\n"),
282 (u8)hdr->boot_metadata_res_entry.flags);
283 tprintf(T("Boot Metadata Offset = %"PRIu64"\n"),
284 hdr->boot_metadata_res_entry.offset);
285 tprintf(T("Boot Metadata Original Size = %"PRIu64"\n"),
286 hdr->boot_metadata_res_entry.original_size);
287 tprintf(T("Boot Index = %u\n"), hdr->boot_idx);
288 tprintf(T("Integrity Size = %"PRIu64"\n"),
289 (u64)hdr->integrity.size);
290 tprintf(T("Integrity Flags = 0x%hhx\n"),
291 (u8)hdr->integrity.flags);
292 tprintf(T("Integrity Offset = %"PRIu64"\n"),
293 hdr->integrity.offset);
294 tprintf(T("Integrity Original_size = %"PRIu64"\n"),
295 hdr->integrity.original_size);