4 * Read, write, or create a WIM header.
8 * Copyright (C) 2010 Carl Thijssen
9 * Copyright (C) 2012 Eric Biggers
11 * This file is part of wimlib, a library for working with WIM files.
13 * wimlib is free software; you can redistribute it and/or modify it under the
14 * terms of the GNU Lesser General Public License as published by the Free
15 * Software Foundation; either version 2.1 of the License, or (at your option)
18 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with wimlib; if not, see http://www.gnu.org/licenses/.
27 #include "wimlib_internal.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. */
35 int read_header(FILE *fp, struct wim_header *hdr, int split_ok)
38 u8 buf[WIM_HEADER_DISK_SIZE];
48 DEBUG("Reading WIM header.\n");
50 bytes_read = fread(buf, 1, WIM_MAGIC_LEN, fp);
52 if (bytes_read != WIM_MAGIC_LEN)
57 if (memcmp(buf, wim_magic_chars, WIM_MAGIC_LEN) != 0) {
58 ERROR("Invalid magic characters in WIM header\n");
59 return WIMLIB_ERR_NOT_A_WIM_FILE;
62 bytes_read = fread(&hdr_size, 1, sizeof(u32), fp);
63 if (bytes_read != sizeof(u32))
70 if (hdr_size != WIM_HEADER_DISK_SIZE) {
71 DEBUG("ERROR: Header is size %u (expected %u)\n",
72 hdr_size, WIM_HEADER_DISK_SIZE);
73 return WIMLIB_ERR_INVALID_HEADER_SIZE;
76 /* Read the rest of the header into a buffer. */
78 hdr_rem_size = WIM_HEADER_DISK_SIZE - WIM_MAGIC_LEN - sizeof(u32);
80 bytes_read = fread(buf + WIM_MAGIC_LEN + sizeof(u32), 1,
82 if (bytes_read != hdr_rem_size)
85 p = get_u32(buf + WIM_MAGIC_LEN + sizeof(u32), &wim_version);
87 if (wim_version != WIM_VERSION) {
88 ERROR("The WIM header says the WIM version is %u, but Wimlib "
89 "only knows about version %u.\n", wim_version,
91 return WIMLIB_ERR_UNKNOWN_VERSION;
94 p = get_u32(p, &hdr->flags);
95 p = get_u32(p, &chunk_size);
96 if (chunk_size != WIM_CHUNK_SIZE &&
97 (hdr->flags & WIM_HDR_FLAG_COMPRESSION)) {
98 ERROR("Unexpected chunk size of %u! Ask the author to "
99 "implement support for other chunk sizes. "
100 "(Or it might just be that the WIM header is "
101 "invalid.)\n", chunk_size);
102 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
105 p = get_bytes(p, WIM_GID_LEN, hdr->guid);
106 p = get_u16(p, &hdr->part_number);
107 p = get_u16(p, &hdr->total_parts);
109 if (!split_ok && (hdr->part_number != 1 || hdr->total_parts != 1)) {
110 ERROR("This WIM is part %u of a %u-part WIM.\n",
111 hdr->part_number, hdr->total_parts);
112 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
115 p = get_u32(p, &hdr->image_count);
117 DEBUG("part_number = %u, total_parts = %u, image_count = %u\n",
118 hdr->part_number, hdr->total_parts, hdr->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 ERROR("Unexpected EOF while reading WIM header!\n");
146 ERROR("Error reading WIM header: %m\n");
147 return WIMLIB_ERR_READ;
151 * Writes the header for a WIM file.
153 * @hdr: A pointer to a struct wim_header structure that describes the header.
154 * @out: The FILE* for the output file, positioned at the appropriate
155 * place (the beginning of the file).
156 * @return: True on success, false on failure.
158 int write_header(const struct wim_header *hdr, FILE *out)
160 u8 buf[WIM_HEADER_DISK_SIZE];
162 DEBUG("Writing WIM header.\n");
164 p = put_bytes(buf, WIM_MAGIC_LEN, wim_magic_chars);
165 p = put_u32(p, WIM_HEADER_DISK_SIZE);
166 p = put_u32(p, WIM_VERSION);
167 p = put_u32(p, hdr->flags);
168 p = put_u32(p, (hdr->flags & WIM_HDR_FLAG_COMPRESSION) ?
172 p = put_bytes(p, WIM_GID_LEN, hdr->guid);
173 p = put_u16(p, hdr->part_number);
177 p = put_u16(p, hdr->total_parts);
178 p = put_u32(p, hdr->image_count);
179 p = put_resource_entry(p, &hdr->lookup_table_res_entry);
180 p = put_resource_entry(p, &hdr->xml_res_entry);
181 p = put_resource_entry(p, &hdr->boot_metadata_res_entry);
182 p = put_u32(p, hdr->boot_idx);
183 p = put_resource_entry(p, &hdr->integrity);
184 memset(p, 0, WIM_UNUSED_LEN);
185 if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
186 DEBUG("Failed to write WIM header: %m\n");
187 return WIMLIB_ERR_WRITE;
193 * Initializes the header for a WIM file.
195 int init_header(struct wim_header *hdr, int ctype)
197 memset(hdr, 0, sizeof(struct wim_header));
199 case WIM_COMPRESSION_TYPE_NONE:
202 case WIM_COMPRESSION_TYPE_LZX:
203 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
204 WIM_HDR_FLAG_COMPRESS_LZX;
206 case WIM_COMPRESSION_TYPE_XPRESS:
207 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
208 WIM_HDR_FLAG_COMPRESS_XPRESS;
211 ERROR("Invalid compression type specified (%d)!\n", 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. */
239 WIMLIBAPI void wimlib_print_header(const WIMStruct *w)
241 const struct wim_header *hdr = &w->hdr;
244 printf("Magic Characters = MSWIM\\000\\000\\000\n");
245 printf("Header Size = %u\n", WIM_HEADER_DISK_SIZE);
246 printf("Version = 0x%x\n", WIM_VERSION);
248 printf("Flags = 0x%x\n", hdr->flags);
249 for (i = 0; i < ARRAY_LEN(hdr_flags); i++)
250 if (hdr_flags[i].flag & hdr->flags)
251 printf(" WIM_HDR_FLAG_%s is set\n", hdr_flags[i].name);
253 printf("Chunk Size = %u\n", WIM_CHUNK_SIZE);
254 fputs ("GUID = ", stdout);
255 print_byte_field(hdr->guid, WIM_GID_LEN);
257 printf("Part Number = %hu\n", 1);
258 printf("Total Parts = %hu\n", 1);
259 printf("Image Count = %u\n", hdr->image_count);
260 printf("Lookup Table Size = %"PRIu64"\n",
261 (u64)hdr->lookup_table_res_entry.size);
262 printf("Lookup Table Flags = 0x%hhx\n",
263 hdr->lookup_table_res_entry.flags);
264 printf("Lookup Table Offset = %"PRIu64"\n",
265 hdr->lookup_table_res_entry.offset);
266 printf("Lookup Table Original_size = %"PRIu64"\n",
267 hdr->lookup_table_res_entry.original_size);
268 printf("XML Data Size = %"PRIu64"\n",
269 (u64)hdr->xml_res_entry.size);
270 printf("XML Data Flags = 0x%hhx\n",
271 hdr->xml_res_entry.flags);
272 printf("XML Data Offset = %"PRIu64"\n",
273 hdr->xml_res_entry.offset);
274 printf("XML Data Original Size = %"PRIu64"\n",
275 hdr->xml_res_entry.original_size);
276 printf("Boot Metadata Size = %"PRIu64"\n",
277 (u64)hdr->boot_metadata_res_entry.size);
278 printf("Boot Metadata Flags = 0x%hhx\n",
279 hdr->boot_metadata_res_entry.flags);
280 printf("Boot Metadata Offset = %"PRIu64"\n",
281 hdr->boot_metadata_res_entry.offset);
282 printf("Boot Metadata Original Size = %"PRIu64"\n",
283 hdr->boot_metadata_res_entry.original_size);
284 printf("Boot Index = %u\n", hdr->boot_idx);
285 printf("Integrity Size = %"PRIu64"\n",
286 (u64)hdr->integrity.size);
287 printf("Integrity Flags = 0x%hhx\n",
288 hdr->integrity.flags);
289 printf("Integrity Offset = %"PRIu64"\n",
290 hdr->integrity.offset);
291 printf("Integrity Original_size = %"PRIu64"\n",
292 hdr->integrity.original_size);