Honor WIM_HDR_FLAG_READONLY
[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,
119             struct wim_header *hdr, int open_flags)
120 {
121         struct wim_header_disk disk_hdr _aligned_attribute(8);
122
123         BUILD_BUG_ON(sizeof(struct wim_header_disk) != WIM_HEADER_DISK_SIZE);
124
125         DEBUG("Reading WIM header from \"%"TS"\"", filename);
126
127         if (full_pread(in_fd, &disk_hdr, sizeof(disk_hdr), 0) != sizeof(disk_hdr)) {
128                 ERROR_WITH_ERRNO("\"%"TS"\": Error reading header", filename);
129                 return WIMLIB_ERR_READ;
130         }
131
132         if (disk_hdr.magic != WIM_MAGIC) {
133                 ERROR("\"%"TS"\": Invalid magic characters in header", filename);
134                 return WIMLIB_ERR_NOT_A_WIM_FILE;
135         }
136
137         if (le32_to_cpu(disk_hdr.hdr_size) != sizeof(struct wim_header_disk)) {
138                 ERROR("\"%"TS"\": Header size is invalid (%u bytes)",
139                       filename, le32_to_cpu(disk_hdr.hdr_size));
140                 return WIMLIB_ERR_INVALID_HEADER_SIZE;
141         }
142
143         if (le32_to_cpu(disk_hdr.wim_version) != WIM_VERSION) {
144                 ERROR("\"%"TS"\": The WIM header says the WIM version is %u, "
145                       "but wimlib only knows about version %u",
146                       filename, le32_to_cpu(disk_hdr.wim_version), WIM_VERSION);
147                 return WIMLIB_ERR_UNKNOWN_VERSION;
148         }
149
150         hdr->flags = le32_to_cpu(disk_hdr.wim_flags);
151         if (le32_to_cpu(disk_hdr.chunk_size) != WIM_CHUNK_SIZE &&
152             (hdr->flags & WIM_HDR_FLAG_COMPRESSION)) {
153                 ERROR("\"%"TS"\": Unexpected chunk size of %u! Ask the author to "
154                       "implement support for other chunk sizes.",
155                       filename, le32_to_cpu(disk_hdr.chunk_size));
156                 ERROR("(Or it might just be that the WIM header is invalid.)");
157                 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
158         }
159
160         memcpy(hdr->guid, disk_hdr.guid, WIM_GID_LEN);
161
162         hdr->part_number = le16_to_cpu(disk_hdr.part_number);
163         hdr->total_parts = le16_to_cpu(disk_hdr.total_parts);
164
165         if (hdr->total_parts == 0 || hdr->part_number == 0 ||
166             hdr->part_number > hdr->total_parts)
167         {
168                 ERROR("\"%"TS"\": Invalid WIM part number: %hu of %hu",
169                       filename, hdr->part_number, hdr->total_parts);
170                 return WIMLIB_ERR_INVALID_PART_NUMBER;
171         }
172
173         if (!(open_flags & WIMLIB_OPEN_FLAG_SPLIT_OK) && hdr->total_parts != 1)
174         {
175                 ERROR("\"%"TS"\": This WIM is part %u of a %u-part WIM",
176                       filename, hdr->part_number, hdr->total_parts);
177                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
178         }
179
180         hdr->image_count = le32_to_cpu(disk_hdr.image_count);
181
182         DEBUG("part_number = %u, total_parts = %u, image_count = %u",
183               hdr->part_number, hdr->total_parts, hdr->image_count);
184
185         if (hdr->image_count >= INT_MAX) {
186                 ERROR("\"%"TS"\": Invalid image count (%u)",
187                       filename, hdr->image_count);
188                 return WIMLIB_ERR_IMAGE_COUNT;
189         }
190
191         get_resource_entry(&disk_hdr.lookup_table_res_entry, &hdr->lookup_table_res_entry);
192         get_resource_entry(&disk_hdr.xml_data_res_entry, &hdr->xml_res_entry);
193         get_resource_entry(&disk_hdr.boot_metadata_res_entry, &hdr->boot_metadata_res_entry);
194         hdr->boot_idx = le32_to_cpu(disk_hdr.boot_idx);
195         get_resource_entry(&disk_hdr.integrity_table_res_entry, &hdr->integrity);
196         return 0;
197 }
198
199 /*
200  * Writes the header for a WIM file.
201  *
202  * @hdr:        A pointer to a struct wim_header structure that describes the header.
203  * @out_fd:     The file descriptor to the WIM file, opened for writing.
204  *
205  * Returns zero on success, nonzero on failure.
206  */
207 int
208 write_header(const struct wim_header *hdr, int out_fd)
209 {
210         struct wim_header_disk disk_hdr _aligned_attribute(8);
211         DEBUG("Writing WIM header.");
212
213         disk_hdr.magic = WIM_MAGIC;
214         disk_hdr.hdr_size = cpu_to_le32(sizeof(struct wim_header_disk));
215         disk_hdr.wim_version = cpu_to_le32(WIM_VERSION);
216         disk_hdr.wim_flags = cpu_to_le32(hdr->flags);
217         disk_hdr.chunk_size = cpu_to_le32((hdr->flags & WIM_HDR_FLAG_COMPRESSION) ?
218                                                 WIM_CHUNK_SIZE : 0);
219         memcpy(disk_hdr.guid, hdr->guid, WIM_GID_LEN);
220
221         disk_hdr.part_number = cpu_to_le16(hdr->part_number);
222         disk_hdr.total_parts = cpu_to_le16(hdr->total_parts);
223         disk_hdr.image_count = cpu_to_le32(hdr->image_count);
224         put_resource_entry(&hdr->lookup_table_res_entry, &disk_hdr.lookup_table_res_entry);
225         put_resource_entry(&hdr->xml_res_entry, &disk_hdr.xml_data_res_entry);
226         put_resource_entry(&hdr->boot_metadata_res_entry, &disk_hdr.boot_metadata_res_entry);
227         disk_hdr.boot_idx = cpu_to_le32(hdr->boot_idx);
228         put_resource_entry(&hdr->integrity, &disk_hdr.integrity_table_res_entry);
229         memset(disk_hdr.unused, 0, sizeof(disk_hdr.unused));
230
231         if (full_pwrite(out_fd, &disk_hdr, sizeof(disk_hdr), 0) != sizeof(disk_hdr)) {
232                 ERROR_WITH_ERRNO("Failed to write WIM header");
233                 return WIMLIB_ERR_WRITE;
234         }
235         DEBUG("Done writing WIM header");
236         return 0;
237 }
238
239 /*
240  * Initializes the header for a WIM file.
241  */
242 int
243 init_header(struct wim_header *hdr, int ctype)
244 {
245         memset(hdr, 0, sizeof(struct wim_header));
246         switch (ctype) {
247         case WIMLIB_COMPRESSION_TYPE_NONE:
248                 hdr->flags = 0;
249                 break;
250         case WIMLIB_COMPRESSION_TYPE_LZX:
251                 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
252                              WIM_HDR_FLAG_COMPRESS_LZX;
253                 break;
254         case WIMLIB_COMPRESSION_TYPE_XPRESS:
255                 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
256                              WIM_HDR_FLAG_COMPRESS_XPRESS;
257                 break;
258         default:
259                 ERROR("Invalid compression type specified (%d)", ctype);
260                 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
261         }
262         hdr->total_parts = 1;
263         hdr->part_number = 1;
264         randomize_byte_array(hdr->guid, sizeof(hdr->guid));
265         return 0;
266 }
267
268 struct hdr_flag {
269         u32 flag;
270         const char *name;
271 };
272 struct hdr_flag hdr_flags[] = {
273         {WIM_HDR_FLAG_RESERVED,         "RESERVED"},
274         {WIM_HDR_FLAG_COMPRESSION,      "COMPRESSION"},
275         {WIM_HDR_FLAG_READONLY,         "READONLY"},
276         {WIM_HDR_FLAG_SPANNED,          "SPANNED"},
277         {WIM_HDR_FLAG_RESOURCE_ONLY,    "RESOURCE_ONLY"},
278         {WIM_HDR_FLAG_METADATA_ONLY,    "METADATA_ONLY"},
279         {WIM_HDR_FLAG_WRITE_IN_PROGRESS,"WRITE_IN_PROGRESS"},
280         {WIM_HDR_FLAG_RP_FIX,           "RP_FIX"},
281         {WIM_HDR_FLAG_COMPRESS_RESERVED,"COMPRESS_RESERVED"},
282         {WIM_HDR_FLAG_COMPRESS_LZX,     "COMPRESS_LZX"},
283         {WIM_HDR_FLAG_COMPRESS_XPRESS,  "COMPRESS_XPRESS"},
284 };
285
286 /* Prints information from the header of the WIM file associated with @w. */
287 WIMLIBAPI void
288 wimlib_print_header(const WIMStruct *w)
289 {
290         const struct wim_header *hdr = &w->hdr;
291
292         tprintf(T("Magic Characters            = MSWIM\\000\\000\\000\n"));
293         tprintf(T("Header Size                 = %u\n"), WIM_HEADER_DISK_SIZE);
294         tprintf(T("Version                     = 0x%x\n"), WIM_VERSION);
295
296         tprintf(T("Flags                       = 0x%x\n"), hdr->flags);
297         for (size_t i = 0; i < ARRAY_LEN(hdr_flags); i++)
298                 if (hdr_flags[i].flag & hdr->flags)
299                         tprintf(T("    WIM_HDR_FLAG_%s is set\n"), hdr_flags[i].name);
300
301         tprintf(T("Chunk Size                  = %u\n"), WIM_CHUNK_SIZE);
302         tfputs (T("GUID                        = "), stdout);
303         print_byte_field(hdr->guid, WIM_GID_LEN, stdout);
304         tputchar(T('\n'));
305         tprintf(T("Part Number                 = %hu\n"), w->hdr.part_number);
306         tprintf(T("Total Parts                 = %hu\n"), w->hdr.total_parts);
307         tprintf(T("Image Count                 = %u\n"), hdr->image_count);
308         tprintf(T("Lookup Table Size           = %"PRIu64"\n"),
309                                 (u64)hdr->lookup_table_res_entry.size);
310         tprintf(T("Lookup Table Flags          = 0x%hhx\n"),
311                                 (u8)hdr->lookup_table_res_entry.flags);
312         tprintf(T("Lookup Table Offset         = %"PRIu64"\n"),
313                                 hdr->lookup_table_res_entry.offset);
314         tprintf(T("Lookup Table Original_size  = %"PRIu64"\n"),
315                                 hdr->lookup_table_res_entry.original_size);
316         tprintf(T("XML Data Size               = %"PRIu64"\n"),
317                                 (u64)hdr->xml_res_entry.size);
318         tprintf(T("XML Data Flags              = 0x%hhx\n"),
319                                 (u8)hdr->xml_res_entry.flags);
320         tprintf(T("XML Data Offset             = %"PRIu64"\n"),
321                                 hdr->xml_res_entry.offset);
322         tprintf(T("XML Data Original Size      = %"PRIu64"\n"),
323                                 hdr->xml_res_entry.original_size);
324         tprintf(T("Boot Metadata Size          = %"PRIu64"\n"),
325                                 (u64)hdr->boot_metadata_res_entry.size);
326         tprintf(T("Boot Metadata Flags         = 0x%hhx\n"),
327                                 (u8)hdr->boot_metadata_res_entry.flags);
328         tprintf(T("Boot Metadata Offset        = %"PRIu64"\n"),
329                                 hdr->boot_metadata_res_entry.offset);
330         tprintf(T("Boot Metadata Original Size = %"PRIu64"\n"),
331                                 hdr->boot_metadata_res_entry.original_size);
332         tprintf(T("Boot Index                  = %u\n"), hdr->boot_idx);
333         tprintf(T("Integrity Size              = %"PRIu64"\n"),
334                                 (u64)hdr->integrity.size);
335         tprintf(T("Integrity Flags             = 0x%hhx\n"),
336                                 (u8)hdr->integrity.flags);
337         tprintf(T("Integrity Offset            = %"PRIu64"\n"),
338                                 hdr->integrity.offset);
339         tprintf(T("Integrity Original_size     = %"PRIu64"\n"),
340                                 hdr->integrity.original_size);
341 }