5d0b683ba7d6ff361f28de36e84f466ff02fa2a9
[wimlib] / src / header.c
1 /*
2  * header.c
3  *
4  * Read, write, or create a WIM header.
5  */
6
7 /*
8  * Copyright (C) 2012, 2013 Eric Biggers
9  *
10  * This file is part of wimlib, a library for working with WIM files.
11  *
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)
15  * any later version.
16  *
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
20  * details.
21  *
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/.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #  include "config.h"
28 #endif
29
30 #include "wimlib.h"
31 #include "wimlib/assert.h"
32 #include "wimlib/endianness.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"
38
39 #include <limits.h>
40 #include <string.h>
41
42 /* WIM magic characters, translated to a single 64-bit little endian number. */
43 #define WIM_MAGIC \
44                 cpu_to_le64(((u64)'M' << 0) |           \
45                             ((u64)'S' << 8) |           \
46                             ((u64)'W' << 16) |          \
47                             ((u64)'I' << 24) |          \
48                             ((u64)'M' << 32) |          \
49                             ((u64)'\0' << 40) |         \
50                             ((u64)'\0' << 48) |         \
51                             ((u64)'\0' << 54))
52
53 /* On-disk format of the WIM header. */
54 struct wim_header_disk {
55
56         /* Magic characters "MSWIM\0\0\0" */
57         le64 magic;
58
59         /* Size of the WIM header, in bytes; WIM_HEADER_DISK_SIZE expected
60          * (currently the only supported value). */
61         u32 hdr_size;
62
63         /* Version of the WIM file; WIM_VERSION expected (currently the only
64          * supported value). */
65         u32 wim_version;
66
67         /* Flags for the WIM file (WIM_HDR_FLAG_*) */
68         u32 wim_flags;
69
70         /* Uncompressed chunk size of resources in the WIM.  0 if the WIM is
71          * uncompressed.  If compressed, WIM_CHUNK_SIZE is expected (currently
72          * the only supported value).  */
73         u32 chunk_size;
74
75         /* Globally unique identifier for the WIM file.  Basically a bunch of
76          * random bytes. */
77         u8 guid[WIM_GID_LEN];
78
79         /* Number of this WIM part in the split WIM file, indexed from 1, or 1
80          * if the WIM is not split. */
81         u16 part_number;
82
83         /* Total number of parts of the split WIM file, or 1 if the WIM is not
84          * split. */
85         u16 total_parts;
86
87         /* Number of images in the WIM. */
88         u32 image_count;
89
90         /* Location and size of the WIM's lookup table. */
91         struct resource_entry_disk lookup_table_res_entry;
92
93         /* Location and size of the WIM's XML data. */
94         struct resource_entry_disk xml_data_res_entry;
95
96         /* Location and size of metadata resource for the bootable image of the
97          * WIM, or all zeroes if no image is bootable. */
98         struct resource_entry_disk boot_metadata_res_entry;
99
100         /* 1-based index of the bootable image of the WIM, or 0 if no image is
101          * bootable. */
102         u32 boot_idx;
103
104         /* Location and size of the WIM's integrity table, or all zeroes if the
105          * WIM has no integrity table.
106          *
107          * Note the integrity_table_res_entry here is 4-byte aligned even though
108          * it would ordinarily be 8-byte aligned--- hence, the _packed_attribute
109          * on the `struct wim_header_disk' is essential. */
110         struct resource_entry_disk integrity_table_res_entry;
111
112         /* Unused bytes. */
113         u8 unused[60];
114 } _packed_attribute;
115
116 /* Reads the header from a WIM file.  */
117 int
118 read_header(const tchar *filename, int in_fd, struct wim_header *hdr)
119 {
120         struct wim_header_disk disk_hdr _aligned_attribute(8);
121
122         BUILD_BUG_ON(sizeof(struct wim_header_disk) != WIM_HEADER_DISK_SIZE);
123
124         DEBUG("Reading WIM header from \"%"TS"\"", filename);
125
126         if (full_pread(in_fd, &disk_hdr, sizeof(disk_hdr), 0) != sizeof(disk_hdr)) {
127                 ERROR_WITH_ERRNO("\"%"TS"\": Error reading header", filename);
128                 return WIMLIB_ERR_READ;
129         }
130
131         if (disk_hdr.magic != WIM_MAGIC) {
132                 ERROR("\"%"TS"\": Invalid magic characters in header", filename);
133                 return WIMLIB_ERR_NOT_A_WIM_FILE;
134         }
135
136         if (le32_to_cpu(disk_hdr.hdr_size) != sizeof(struct wim_header_disk)) {
137                 ERROR("\"%"TS"\": Header size is invalid (%u bytes)",
138                       filename, le32_to_cpu(disk_hdr.hdr_size));
139                 return WIMLIB_ERR_INVALID_HEADER_SIZE;
140         }
141
142         if (le32_to_cpu(disk_hdr.wim_version) != WIM_VERSION) {
143                 ERROR("\"%"TS"\": The WIM header says the WIM version is %u, "
144                       "but wimlib only knows about version %u",
145                       filename, le32_to_cpu(disk_hdr.wim_version), WIM_VERSION);
146                 return WIMLIB_ERR_UNKNOWN_VERSION;
147         }
148
149         hdr->flags = le32_to_cpu(disk_hdr.wim_flags);
150         if (le32_to_cpu(disk_hdr.chunk_size) != WIM_CHUNK_SIZE &&
151             (hdr->flags & WIM_HDR_FLAG_COMPRESSION)) {
152                 ERROR("\"%"TS"\": Unexpected chunk size of %u! Ask the author to "
153                       "implement support for other chunk sizes.",
154                       filename, le32_to_cpu(disk_hdr.chunk_size));
155                 ERROR("(Or it might just be that the WIM header is invalid.)");
156                 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
157         }
158
159         memcpy(hdr->guid, disk_hdr.guid, WIM_GID_LEN);
160
161         hdr->part_number = le16_to_cpu(disk_hdr.part_number);
162         hdr->total_parts = le16_to_cpu(disk_hdr.total_parts);
163
164         if (hdr->total_parts == 0 || hdr->part_number == 0 ||
165             hdr->part_number > hdr->total_parts)
166         {
167                 ERROR("\"%"TS"\": Invalid WIM part number: %hu of %hu",
168                       filename, hdr->part_number, hdr->total_parts);
169                 return WIMLIB_ERR_INVALID_PART_NUMBER;
170         }
171
172         hdr->image_count = le32_to_cpu(disk_hdr.image_count);
173
174         DEBUG("part_number = %u, total_parts = %u, image_count = %u",
175               hdr->part_number, hdr->total_parts, hdr->image_count);
176
177         if (hdr->image_count >= INT_MAX) {
178                 ERROR("\"%"TS"\": Invalid image count (%u)",
179                       filename, hdr->image_count);
180                 return WIMLIB_ERR_IMAGE_COUNT;
181         }
182
183         get_resource_entry(&disk_hdr.lookup_table_res_entry, &hdr->lookup_table_res_entry);
184         get_resource_entry(&disk_hdr.xml_data_res_entry, &hdr->xml_res_entry);
185         get_resource_entry(&disk_hdr.boot_metadata_res_entry, &hdr->boot_metadata_res_entry);
186         hdr->boot_idx = le32_to_cpu(disk_hdr.boot_idx);
187         get_resource_entry(&disk_hdr.integrity_table_res_entry, &hdr->integrity);
188         return 0;
189 }
190
191 /*
192  * Writes the header for a WIM file.
193  *
194  * @hdr:        A pointer to a struct wim_header structure that describes the header.
195  * @out_fd:     The file descriptor to the WIM file, opened for writing.
196  *
197  * Returns zero on success, nonzero on failure.
198  */
199 int
200 write_header(const struct wim_header *hdr, int out_fd)
201 {
202         struct wim_header_disk disk_hdr _aligned_attribute(8);
203         DEBUG("Writing WIM header.");
204
205         disk_hdr.magic = WIM_MAGIC;
206         disk_hdr.hdr_size = cpu_to_le32(sizeof(struct wim_header_disk));
207         disk_hdr.wim_version = cpu_to_le32(WIM_VERSION);
208         disk_hdr.wim_flags = cpu_to_le32(hdr->flags);
209         disk_hdr.chunk_size = cpu_to_le32((hdr->flags & WIM_HDR_FLAG_COMPRESSION) ?
210                                                 WIM_CHUNK_SIZE : 0);
211         memcpy(disk_hdr.guid, hdr->guid, WIM_GID_LEN);
212
213         disk_hdr.part_number = cpu_to_le16(hdr->part_number);
214         disk_hdr.total_parts = cpu_to_le16(hdr->total_parts);
215         disk_hdr.image_count = cpu_to_le32(hdr->image_count);
216         put_resource_entry(&hdr->lookup_table_res_entry, &disk_hdr.lookup_table_res_entry);
217         put_resource_entry(&hdr->xml_res_entry, &disk_hdr.xml_data_res_entry);
218         put_resource_entry(&hdr->boot_metadata_res_entry, &disk_hdr.boot_metadata_res_entry);
219         disk_hdr.boot_idx = cpu_to_le32(hdr->boot_idx);
220         put_resource_entry(&hdr->integrity, &disk_hdr.integrity_table_res_entry);
221         memset(disk_hdr.unused, 0, sizeof(disk_hdr.unused));
222
223         if (full_pwrite(out_fd, &disk_hdr, sizeof(disk_hdr), 0) != sizeof(disk_hdr)) {
224                 ERROR_WITH_ERRNO("Failed to write WIM header");
225                 return WIMLIB_ERR_WRITE;
226         }
227         DEBUG("Done writing WIM header");
228         return 0;
229 }
230
231 /* Update just the wim_flags field. */
232 int
233 write_header_flags(u32 hdr_flags, int out_fd)
234 {
235         le32 flags = cpu_to_le32(hdr_flags);
236         if (full_pwrite(out_fd, &flags, sizeof(flags),
237                         offsetof(struct wim_header_disk, wim_flags)) != sizeof(flags))
238         {
239                 return WIMLIB_ERR_WRITE;
240         } else {
241                 return 0;
242         }
243
244 }
245
246 /* Update just the part_number and total_parts fields. */
247 int
248 write_header_part_data(u16 part_number, u16 total_parts, int out_fd)
249 {
250         le16 part_data[2] = {
251                 cpu_to_le16(part_number),
252                 cpu_to_le16(total_parts),
253         };
254         if (full_pwrite(out_fd, &part_data, sizeof(part_data),
255                         offsetof(struct wim_header_disk, part_number)) != sizeof(part_data))
256         {
257                 return WIMLIB_ERR_WRITE;
258         } else {
259                 return 0;
260         }
261 }
262
263 /*
264  * Initializes the header for a WIM file.
265  */
266 int
267 init_header(struct wim_header *hdr, int ctype)
268 {
269         memset(hdr, 0, sizeof(struct wim_header));
270         switch (ctype) {
271         case WIMLIB_COMPRESSION_TYPE_NONE:
272                 hdr->flags = 0;
273                 break;
274         case WIMLIB_COMPRESSION_TYPE_LZX:
275                 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
276                              WIM_HDR_FLAG_COMPRESS_LZX;
277                 break;
278         case WIMLIB_COMPRESSION_TYPE_XPRESS:
279                 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
280                              WIM_HDR_FLAG_COMPRESS_XPRESS;
281                 break;
282         default:
283                 ERROR("Invalid compression type specified (%d)", ctype);
284                 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
285         }
286         hdr->total_parts = 1;
287         hdr->part_number = 1;
288         randomize_byte_array(hdr->guid, sizeof(hdr->guid));
289         return 0;
290 }
291
292 struct hdr_flag {
293         u32 flag;
294         const char *name;
295 };
296 struct hdr_flag hdr_flags[] = {
297         {WIM_HDR_FLAG_RESERVED,         "RESERVED"},
298         {WIM_HDR_FLAG_COMPRESSION,      "COMPRESSION"},
299         {WIM_HDR_FLAG_READONLY,         "READONLY"},
300         {WIM_HDR_FLAG_SPANNED,          "SPANNED"},
301         {WIM_HDR_FLAG_RESOURCE_ONLY,    "RESOURCE_ONLY"},
302         {WIM_HDR_FLAG_METADATA_ONLY,    "METADATA_ONLY"},
303         {WIM_HDR_FLAG_WRITE_IN_PROGRESS,"WRITE_IN_PROGRESS"},
304         {WIM_HDR_FLAG_RP_FIX,           "RP_FIX"},
305         {WIM_HDR_FLAG_COMPRESS_RESERVED,"COMPRESS_RESERVED"},
306         {WIM_HDR_FLAG_COMPRESS_LZX,     "COMPRESS_LZX"},
307         {WIM_HDR_FLAG_COMPRESS_XPRESS,  "COMPRESS_XPRESS"},
308 };
309
310 /* Prints information from the header of the WIM file associated with @w. */
311 WIMLIBAPI void
312 wimlib_print_header(const WIMStruct *w)
313 {
314         const struct wim_header *hdr = &w->hdr;
315
316         tprintf(T("Magic Characters            = MSWIM\\000\\000\\000\n"));
317         tprintf(T("Header Size                 = %u\n"), WIM_HEADER_DISK_SIZE);
318         tprintf(T("Version                     = 0x%x\n"), WIM_VERSION);
319
320         tprintf(T("Flags                       = 0x%x\n"), hdr->flags);
321         for (size_t i = 0; i < ARRAY_LEN(hdr_flags); i++)
322                 if (hdr_flags[i].flag & hdr->flags)
323                         tprintf(T("    WIM_HDR_FLAG_%s is set\n"), hdr_flags[i].name);
324
325         tprintf(T("Chunk Size                  = %u\n"), WIM_CHUNK_SIZE);
326         tfputs (T("GUID                        = "), stdout);
327         print_byte_field(hdr->guid, WIM_GID_LEN, stdout);
328         tputchar(T('\n'));
329         tprintf(T("Part Number                 = %hu\n"), w->hdr.part_number);
330         tprintf(T("Total Parts                 = %hu\n"), w->hdr.total_parts);
331         tprintf(T("Image Count                 = %u\n"), hdr->image_count);
332         tprintf(T("Lookup Table Size           = %"PRIu64"\n"),
333                                 (u64)hdr->lookup_table_res_entry.size);
334         tprintf(T("Lookup Table Flags          = 0x%hhx\n"),
335                                 (u8)hdr->lookup_table_res_entry.flags);
336         tprintf(T("Lookup Table Offset         = %"PRIu64"\n"),
337                                 hdr->lookup_table_res_entry.offset);
338         tprintf(T("Lookup Table Original_size  = %"PRIu64"\n"),
339                                 hdr->lookup_table_res_entry.original_size);
340         tprintf(T("XML Data Size               = %"PRIu64"\n"),
341                                 (u64)hdr->xml_res_entry.size);
342         tprintf(T("XML Data Flags              = 0x%hhx\n"),
343                                 (u8)hdr->xml_res_entry.flags);
344         tprintf(T("XML Data Offset             = %"PRIu64"\n"),
345                                 hdr->xml_res_entry.offset);
346         tprintf(T("XML Data Original Size      = %"PRIu64"\n"),
347                                 hdr->xml_res_entry.original_size);
348         tprintf(T("Boot Metadata Size          = %"PRIu64"\n"),
349                                 (u64)hdr->boot_metadata_res_entry.size);
350         tprintf(T("Boot Metadata Flags         = 0x%hhx\n"),
351                                 (u8)hdr->boot_metadata_res_entry.flags);
352         tprintf(T("Boot Metadata Offset        = %"PRIu64"\n"),
353                                 hdr->boot_metadata_res_entry.offset);
354         tprintf(T("Boot Metadata Original Size = %"PRIu64"\n"),
355                                 hdr->boot_metadata_res_entry.original_size);
356         tprintf(T("Boot Index                  = %u\n"), hdr->boot_idx);
357         tprintf(T("Integrity Size              = %"PRIu64"\n"),
358                                 (u64)hdr->integrity.size);
359         tprintf(T("Integrity Flags             = 0x%hhx\n"),
360                                 (u8)hdr->integrity.flags);
361         tprintf(T("Integrity Offset            = %"PRIu64"\n"),
362                                 hdr->integrity.offset);
363         tprintf(T("Integrity Original_size     = %"PRIu64"\n"),
364                                 hdr->integrity.original_size);
365 }