]> wimlib.net Git - wimlib/blob - src/header.c
ff3bb95b016a8e8a8193f4fca4e265882cb9c984
[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 #include "wimlib_internal.h"
27 #include "buffer_io.h"
28 #include <limits.h>
29
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' };
33
34 /* Reads the header for a WIM file.  */
35 int
36 read_header(filedes_t in_fd, struct wim_header *hdr, int open_flags)
37 {
38         size_t bytes_read;
39         u8 buf[WIM_HEADER_DISK_SIZE];
40         const u8 *p;
41
42         u32 hdr_size;
43         u32 wim_version;
44         u32 chunk_size;
45
46         DEBUG("Reading WIM header.");
47
48         bytes_read = full_read(in_fd, buf, WIM_HEADER_DISK_SIZE);
49
50         if (bytes_read != WIM_HEADER_DISK_SIZE) {
51                 ERROR_WITH_ERRNO("Error reading WIM header");
52                 return WIMLIB_ERR_READ;
53         }
54
55         /* Byte 8 */
56
57         p = buf;
58
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;
62         }
63
64         p += 8;
65         p = get_u32(p, &hdr_size);
66
67         /* Byte 12 */
68
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;
73         }
74
75         p = get_u32(buf + WIM_MAGIC_LEN + sizeof(u32), &wim_version);
76
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;
82         }
83
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.",
90                       chunk_size);
91                 ERROR("(Or it might just be that the WIM header is invalid.)");
92                 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
93         }
94
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);
98
99         if (hdr->total_parts == 0 ||
100             hdr->part_number == 0 ||
101             hdr->part_number > hdr->total_parts)
102         {
103                 ERROR("Invalid WIM part number: %hu of %hu",
104                       hdr->part_number, hdr->total_parts);
105                 return WIMLIB_ERR_INVALID_PART_NUMBER;
106         }
107
108         if (!(open_flags & WIMLIB_OPEN_FLAG_SPLIT_OK) &&
109             hdr->total_parts != 1)
110         {
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;
114         }
115
116         p = get_u32(p, &hdr->image_count);
117
118         DEBUG("part_number = %u, total_parts = %u, image_count = %u",
119               hdr->part_number, hdr->total_parts, hdr->image_count);
120
121         if (hdr->image_count >= INT_MAX) {
122                 ERROR("Invalid image count (%u)", hdr->image_count);
123                 return WIMLIB_ERR_IMAGE_COUNT;
124         }
125
126         /* Byte 48 */
127
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);
131
132         /* Byte 120 */
133
134         p = get_u32(p, &hdr->boot_idx);
135
136         /* Byte 124 */
137
138         p = get_resource_entry(p, &hdr->integrity);
139
140         /* Byte 148 */
141
142         /* 60 bytes of unused stuff. */
143
144         /* Byte 208 */
145
146         return 0;
147 }
148
149 /*
150  * Writes the header for a WIM file.
151  *
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.
155  */
156 int
157 write_header(const struct wim_header *hdr, int out_fd)
158 {
159         u8 buf[WIM_HEADER_DISK_SIZE];
160         u8 *p;
161         DEBUG("Writing WIM header.");
162
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) ?
168                                 WIM_CHUNK_SIZE : 0);
169         /* byte 24 */
170
171         p = put_bytes(p, WIM_GID_LEN, hdr->guid);
172         p = put_u16(p, hdr->part_number);
173
174         /* byte 40 */
175
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));
185
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;
189         }
190         DEBUG("Done writing WIM header");
191         return 0;
192 }
193
194 /*
195  * Initializes the header for a WIM file.
196  */
197 int
198 init_header(struct wim_header *hdr, int ctype)
199 {
200         memset(hdr, 0, sizeof(struct wim_header));
201         switch (ctype) {
202         case WIMLIB_COMPRESSION_TYPE_NONE:
203                 hdr->flags = 0;
204                 break;
205         case WIMLIB_COMPRESSION_TYPE_LZX:
206                 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
207                              WIM_HDR_FLAG_COMPRESS_LZX;
208                 break;
209         case WIMLIB_COMPRESSION_TYPE_XPRESS:
210                 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
211                              WIM_HDR_FLAG_COMPRESS_XPRESS;
212                 break;
213         default:
214                 ERROR("Invalid compression type specified (%d)", ctype);
215                 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
216         }
217         hdr->total_parts = 1;
218         hdr->part_number = 1;
219         randomize_byte_array(hdr->guid, sizeof(hdr->guid));
220         return 0;
221 }
222
223 struct hdr_flag {
224         u32 flag;
225         const char *name;
226 };
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"},
239 };
240
241 /* Prints information from the header of the WIM file associated with @w. */
242 WIMLIBAPI void
243 wimlib_print_header(const WIMStruct *w)
244 {
245         const struct wim_header *hdr = &w->hdr;
246
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);
250
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);
255
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);
259         tputchar(T('\n'));
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);
296 }