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(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_pread(in_fd, buf, WIM_HEADER_DISK_SIZE, 0);
50 if (bytes_read != WIM_HEADER_DISK_SIZE) {
51 ERROR_WITH_ERRNO("Error reading WIM header");
52 return WIMLIB_ERR_READ;
56 if (memcmp(p, wim_magic_chars, WIM_MAGIC_LEN)) {
57 ERROR("Invalid magic characters in WIM header");
58 return WIMLIB_ERR_NOT_A_WIM_FILE;
62 p = get_u32(p + 8, &hdr_size);
63 if (hdr_size != WIM_HEADER_DISK_SIZE) {
64 ERROR("Header is %u bytes (expected %u bytes)",
65 hdr_size, WIM_HEADER_DISK_SIZE);
66 return WIMLIB_ERR_INVALID_HEADER_SIZE;
70 p = get_u32(buf + WIM_MAGIC_LEN + sizeof(u32), &wim_version);
71 if (wim_version != WIM_VERSION) {
72 ERROR("The WIM header says the WIM version is %u, but wimlib "
73 "only knows about version %u",
74 wim_version, WIM_VERSION);
75 return WIMLIB_ERR_UNKNOWN_VERSION;
78 p = get_u32(p, &hdr->flags);
79 p = get_u32(p, &chunk_size);
80 if (chunk_size != WIM_CHUNK_SIZE &&
81 (hdr->flags & WIM_HDR_FLAG_COMPRESSION)) {
82 ERROR("Unexpected chunk size of %u! Ask the author to "
83 "implement support for other chunk sizes.",
85 ERROR("(Or it might just be that the WIM header is invalid.)");
86 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
89 p = get_bytes(p, WIM_GID_LEN, hdr->guid);
90 p = get_u16(p, &hdr->part_number);
91 p = get_u16(p, &hdr->total_parts);
93 if (hdr->total_parts == 0 ||
94 hdr->part_number == 0 ||
95 hdr->part_number > hdr->total_parts)
97 ERROR("Invalid WIM part number: %hu of %hu",
98 hdr->part_number, hdr->total_parts);
99 return WIMLIB_ERR_INVALID_PART_NUMBER;
102 if (!(open_flags & WIMLIB_OPEN_FLAG_SPLIT_OK) &&
103 hdr->total_parts != 1)
105 ERROR("This WIM is part %u of a %u-part WIM",
106 hdr->part_number, hdr->total_parts);
107 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
110 p = get_u32(p, &hdr->image_count);
112 DEBUG("part_number = %u, total_parts = %u, image_count = %u",
113 hdr->part_number, hdr->total_parts, hdr->image_count);
115 if (hdr->image_count >= INT_MAX) {
116 ERROR("Invalid image count (%u)", hdr->image_count);
117 return WIMLIB_ERR_IMAGE_COUNT;
122 p = get_resource_entry(p, &hdr->lookup_table_res_entry);
123 p = get_resource_entry(p, &hdr->xml_res_entry);
124 p = get_resource_entry(p, &hdr->boot_metadata_res_entry);
128 p = get_u32(p, &hdr->boot_idx);
132 p = get_resource_entry(p, &hdr->integrity);
136 /* 60 bytes of unused stuff. */
144 * Writes the header for a WIM file.
146 * @hdr: A pointer to a struct wim_header structure that describes the header.
147 * @out_fd: The file descriptor to the WIM file, opened for writing.
149 * Returns zero on success, nonzero on failure.
152 write_header(const struct wim_header *hdr, int out_fd)
154 u8 buf[WIM_HEADER_DISK_SIZE];
156 DEBUG("Writing WIM header.");
158 p = put_bytes(buf, WIM_MAGIC_LEN, wim_magic_chars);
159 p = put_u32(p, WIM_HEADER_DISK_SIZE);
160 p = put_u32(p, WIM_VERSION);
161 p = put_u32(p, hdr->flags);
162 p = put_u32(p, (hdr->flags & WIM_HDR_FLAG_COMPRESSION) ?
166 p = put_bytes(p, WIM_GID_LEN, hdr->guid);
167 p = put_u16(p, hdr->part_number);
171 p = put_u16(p, hdr->total_parts);
172 p = put_u32(p, hdr->image_count);
173 p = put_resource_entry(p, &hdr->lookup_table_res_entry);
174 p = put_resource_entry(p, &hdr->xml_res_entry);
175 p = put_resource_entry(p, &hdr->boot_metadata_res_entry);
176 p = put_u32(p, hdr->boot_idx);
177 p = put_resource_entry(p, &hdr->integrity);
178 p = put_zeroes(p, WIM_UNUSED_LEN);
179 wimlib_assert(p - buf == sizeof(buf));
181 if (full_pwrite(out_fd, buf, sizeof(buf), 0) != sizeof(buf)) {
182 ERROR_WITH_ERRNO("Failed to write WIM header");
183 return WIMLIB_ERR_WRITE;
185 DEBUG("Done writing WIM header");
190 * Initializes the header for a WIM file.
193 init_header(struct wim_header *hdr, int ctype)
195 memset(hdr, 0, sizeof(struct wim_header));
197 case WIMLIB_COMPRESSION_TYPE_NONE:
200 case WIMLIB_COMPRESSION_TYPE_LZX:
201 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
202 WIM_HDR_FLAG_COMPRESS_LZX;
204 case WIMLIB_COMPRESSION_TYPE_XPRESS:
205 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
206 WIM_HDR_FLAG_COMPRESS_XPRESS;
209 ERROR("Invalid compression type specified (%d)", ctype);
210 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
212 hdr->total_parts = 1;
213 hdr->part_number = 1;
214 randomize_byte_array(hdr->guid, sizeof(hdr->guid));
222 struct hdr_flag hdr_flags[] = {
223 {WIM_HDR_FLAG_RESERVED, "RESERVED"},
224 {WIM_HDR_FLAG_COMPRESSION, "COMPRESSION"},
225 {WIM_HDR_FLAG_READONLY, "READONLY"},
226 {WIM_HDR_FLAG_SPANNED, "SPANNED"},
227 {WIM_HDR_FLAG_RESOURCE_ONLY, "RESOURCE_ONLY"},
228 {WIM_HDR_FLAG_METADATA_ONLY, "METADATA_ONLY"},
229 {WIM_HDR_FLAG_WRITE_IN_PROGRESS,"WRITE_IN_PROGRESS"},
230 {WIM_HDR_FLAG_RP_FIX, "RP_FIX"},
231 {WIM_HDR_FLAG_COMPRESS_RESERVED,"COMPRESS_RESERVED"},
232 {WIM_HDR_FLAG_COMPRESS_LZX, "COMPRESS_LZX"},
233 {WIM_HDR_FLAG_COMPRESS_XPRESS, "COMPRESS_XPRESS"},
236 /* Prints information from the header of the WIM file associated with @w. */
238 wimlib_print_header(const WIMStruct *w)
240 const struct wim_header *hdr = &w->hdr;
242 tprintf(T("Magic Characters = MSWIM\\000\\000\\000\n"));
243 tprintf(T("Header Size = %u\n"), WIM_HEADER_DISK_SIZE);
244 tprintf(T("Version = 0x%x\n"), WIM_VERSION);
246 tprintf(T("Flags = 0x%x\n"), hdr->flags);
247 for (size_t i = 0; i < ARRAY_LEN(hdr_flags); i++)
248 if (hdr_flags[i].flag & hdr->flags)
249 tprintf(T(" WIM_HDR_FLAG_%s is set\n"), hdr_flags[i].name);
251 tprintf(T("Chunk Size = %u\n"), WIM_CHUNK_SIZE);
252 tfputs (T("GUID = "), stdout);
253 print_byte_field(hdr->guid, WIM_GID_LEN, stdout);
255 tprintf(T("Part Number = %hu\n"), w->hdr.part_number);
256 tprintf(T("Total Parts = %hu\n"), w->hdr.total_parts);
257 tprintf(T("Image Count = %u\n"), hdr->image_count);
258 tprintf(T("Lookup Table Size = %"PRIu64"\n"),
259 (u64)hdr->lookup_table_res_entry.size);
260 tprintf(T("Lookup Table Flags = 0x%hhx\n"),
261 (u8)hdr->lookup_table_res_entry.flags);
262 tprintf(T("Lookup Table Offset = %"PRIu64"\n"),
263 hdr->lookup_table_res_entry.offset);
264 tprintf(T("Lookup Table Original_size = %"PRIu64"\n"),
265 hdr->lookup_table_res_entry.original_size);
266 tprintf(T("XML Data Size = %"PRIu64"\n"),
267 (u64)hdr->xml_res_entry.size);
268 tprintf(T("XML Data Flags = 0x%hhx\n"),
269 (u8)hdr->xml_res_entry.flags);
270 tprintf(T("XML Data Offset = %"PRIu64"\n"),
271 hdr->xml_res_entry.offset);
272 tprintf(T("XML Data Original Size = %"PRIu64"\n"),
273 hdr->xml_res_entry.original_size);
274 tprintf(T("Boot Metadata Size = %"PRIu64"\n"),
275 (u64)hdr->boot_metadata_res_entry.size);
276 tprintf(T("Boot Metadata Flags = 0x%hhx\n"),
277 (u8)hdr->boot_metadata_res_entry.flags);
278 tprintf(T("Boot Metadata Offset = %"PRIu64"\n"),
279 hdr->boot_metadata_res_entry.offset);
280 tprintf(T("Boot Metadata Original Size = %"PRIu64"\n"),
281 hdr->boot_metadata_res_entry.original_size);
282 tprintf(T("Boot Index = %u\n"), hdr->boot_idx);
283 tprintf(T("Integrity Size = %"PRIu64"\n"),
284 (u64)hdr->integrity.size);
285 tprintf(T("Integrity Flags = 0x%hhx\n"),
286 (u8)hdr->integrity.flags);
287 tprintf(T("Integrity Offset = %"PRIu64"\n"),
288 hdr->integrity.offset);
289 tprintf(T("Integrity Original_size = %"PRIu64"\n"),
290 hdr->integrity.original_size);