4 * Support for creating WIMBoot pointer files.
6 * See http://technet.microsoft.com/en-us/library/dn594399.aspx for general
7 * information about WIMBoot.
9 * Note that WIMBoot pointer files are actually implemented on top of the
10 * Windows Overlay File System Filter (WOF). See wof.h for more info.
14 * Copyright (C) 2014 Eric Biggers
16 * This file is free software; you can redistribute it and/or modify it under
17 * the terms of the GNU Lesser General Public License as published by the Free
18 * Software Foundation; either version 3 of the License, or (at your option) any
21 * This file is distributed in the hope that it will be useful, but WITHOUT
22 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
23 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
26 * You should have received a copy of the GNU Lesser General Public License
27 * along with this file; if not, see http://www.gnu.org/licenses/.
36 #include "wimlib/win32_common.h"
37 #include "wimlib/win32.h"
38 #include "wimlib/assert.h"
39 #include "wimlib/error.h"
40 #include "wimlib/lookup_table.h"
41 #include "wimlib/util.h"
42 #include "wimlib/wimboot.h"
43 #include "wimlib/wof.h"
46 open_file(const wchar_t *device_name, DWORD desiredAccess)
48 return CreateFile(device_name, desiredAccess,
49 FILE_SHARE_VALID_FLAGS, NULL, OPEN_EXISTING,
50 FILE_FLAG_BACKUP_SEMANTICS, NULL);
54 query_device(HANDLE h, DWORD code, void *out, DWORD out_size)
57 return DeviceIoControl(h, code, NULL, 0, out, out_size,
58 &bytes_returned, NULL);
62 * Gets partition and drive information for the specified path.
65 * Absolute path which must begin with a drive letter. For example, if the
66 * path is D:\install.wim, this function will query information about the
69 * Partition info is returned here.
71 * Drive info is returned here. The contained partition info will not be
74 * Returns 0 on success, or a positive error code on failure.
77 query_partition_and_disk_info(const wchar_t *path,
78 PARTITION_INFORMATION_EX *part_info_ret,
79 DRIVE_LAYOUT_INFORMATION_EX *drive_info_ret)
82 wchar_t vol_name[] = L"\\\\.\\X:";
83 wchar_t disk_name[] = L"\\\\?\\PhysicalDriveXXXXXXXXXX";
85 PARTITION_INFORMATION_EX part_info;
86 size_t extents_size = sizeof(VOLUME_DISK_EXTENTS) + 4 * sizeof(DISK_EXTENT);
87 VOLUME_DISK_EXTENTS *extents = alloca(extents_size);
88 size_t drive_info_size = sizeof(DRIVE_LAYOUT_INFORMATION_EX) +
89 8 * sizeof(PARTITION_INFORMATION_EX);
90 DRIVE_LAYOUT_INFORMATION_EX *drive_info = alloca(drive_info_size);
92 wimlib_assert(path[0] != L'\0' && path[1] == L':');
94 *(wcschr(vol_name, L'X')) = path[0];
96 h = open_file(vol_name, GENERIC_READ);
97 if (h == INVALID_HANDLE_VALUE) {
98 set_errno_from_GetLastError();
99 ERROR_WITH_ERRNO("\"%ls\": Can't open volume device", vol_name);
100 return WIMLIB_ERR_OPEN;
103 if (!query_device(h, IOCTL_DISK_GET_PARTITION_INFO_EX,
104 &part_info, sizeof(part_info)))
106 ERROR("\"%ls\": Can't get partition info (err=0x%08"PRIx32")",
107 vol_name, (u32)GetLastError());
109 return WIMLIB_ERR_READ;
112 if (!query_device(h, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
113 extents, extents_size))
115 ERROR("\"%ls\": Can't get volume extent info (err=0x%08"PRIx32")",
116 vol_name, (u32)GetLastError());
118 return WIMLIB_ERR_READ;
123 if (extents->NumberOfDiskExtents != 1) {
124 ERROR("\"%ls\": This volume has %"PRIu32" disk extents, "
125 "but this code is untested for more than 1",
126 vol_name, (u32)extents->NumberOfDiskExtents);
127 return WIMLIB_ERR_UNSUPPORTED;
130 wsprintf(wcschr(disk_name, L'X'), L"%"PRIu32,
131 extents->Extents[0].DiskNumber);
133 h = open_file(disk_name, GENERIC_READ);
134 if (h == INVALID_HANDLE_VALUE) {
135 set_errno_from_GetLastError();
136 ERROR_WITH_ERRNO("\"%ls\": Can't open disk device", disk_name);
137 return WIMLIB_ERR_OPEN;
140 if (!query_device(h, IOCTL_DISK_GET_DRIVE_LAYOUT_EX,
141 drive_info, drive_info_size))
143 ERROR("\"%ls\": Can't get disk info (err=0x%08"PRIx32")",
144 disk_name, (u32)GetLastError());
146 return WIMLIB_ERR_READ;
151 if (drive_info->PartitionStyle != part_info.PartitionStyle) {
152 ERROR("\"%ls\", \"%ls\": Inconsistent partition table type!",
153 vol_name, disk_name);
154 return WIMLIB_ERR_UNSUPPORTED;
157 if (part_info.PartitionStyle == PARTITION_STYLE_GPT) {
158 BUILD_BUG_ON(sizeof(part_info.Gpt.PartitionId) !=
159 sizeof(drive_info->Gpt.DiskId));
160 if (!memcmp(&part_info.Gpt.PartitionId,
161 &drive_info->Gpt.DiskId,
162 sizeof(drive_info->Gpt.DiskId)))
164 ERROR("\"%ls\", \"%ls\": Partition GUID is the "
165 "same as the disk GUID???", vol_name, disk_name);
166 return WIMLIB_ERR_UNSUPPORTED;
170 if (part_info.PartitionStyle != PARTITION_STYLE_MBR &&
171 part_info.PartitionStyle != PARTITION_STYLE_GPT)
173 ERROR("\"%ls\": Unknown partition style 0x%08"PRIx32,
174 vol_name, (u32)part_info.PartitionStyle);
175 return WIMLIB_ERR_UNSUPPORTED;
178 *part_info_ret = part_info;
179 *drive_info_ret = *drive_info;
184 * Allocate a new WIM data source ID.
187 * Previous WimOverlay.dat contents, or NULL if file did not exist.
189 * Returns the new data source ID.
192 alloc_new_data_source_id(const struct WimOverlay_dat_header *old_hdr)
197 for (u64 id = 0; ; id++) {
198 for (u32 i = 0; i < old_hdr->num_entries_1; i++)
199 if (id == old_hdr->entry_1s[i].data_source_id)
208 * Calculate the size of WimOverlay.dat with one entry added.
211 * Previous WimOverlay.dat contents, or NULL if file did not exist.
213 * Size of entry_2 being added.
215 * Size will be returned here.
217 * Returns 0 on success, or WIMLIB_ERR_UNSUPPORTED if size overflows 32 bits.
220 calculate_wimoverlay_dat_size(const struct WimOverlay_dat_header *old_hdr,
221 u32 new_entry_2_size, u32 *size_ret)
226 size_64 = sizeof(struct WimOverlay_dat_header);
228 for (u32 i = 0; i < old_hdr->num_entries_1; i++) {
229 size_64 += sizeof(struct WimOverlay_dat_entry_1);
230 size_64 += old_hdr->entry_1s[i].entry_2_length;
233 size_64 += sizeof(struct WimOverlay_dat_entry_1);
234 size_64 += new_entry_2_size;
238 return WIMLIB_ERR_UNSUPPORTED;
245 * Writes @size bytes of @contents to the named file @path.
247 * Returns 0 on success; WIMLIB_ERR_OPEN or WIMLIB_ERR_WRITE on failure.
250 write_wimoverlay_dat(const wchar_t *path, const void *contents, u32 size)
255 h = CreateFile(path, GENERIC_WRITE, 0, NULL,
256 CREATE_ALWAYS, FILE_FLAG_BACKUP_SEMANTICS, NULL);
257 if (h == INVALID_HANDLE_VALUE) {
258 set_errno_from_GetLastError();
259 ERROR_WITH_ERRNO("\"%ls\": Can't open file for writing", path);
260 return WIMLIB_ERR_OPEN;
263 if (!WriteFile(h, contents, size, &bytes_written, NULL) ||
264 bytes_written != size)
266 set_errno_from_GetLastError();
267 ERROR_WITH_ERRNO("\"%ls\": Can't write file", path);
269 return WIMLIB_ERR_WRITE;
272 if (!CloseHandle(h)) {
273 set_errno_from_GetLastError();
274 ERROR_WITH_ERRNO("\"%ls\": Can't close handle", path);
275 return WIMLIB_ERR_WRITE;
282 * Generates the contents of WimOverlay.dat in memory, with one entry added.
285 * Buffer large enough to hold the new contents.
287 * Old contents of WimOverlay.dat, or NULL if it did not exist.
289 * Absolute path to the WIM file. It must begin with a drive letter; for
290 * example, D:\install.wim.
292 * GUID of the WIM, from the WIM header.
294 * Number of the image in the WIM to specify in the new entry.
295 * @new_data_source_id
296 * Data source ID to use for the new entry.
298 * Partition information for the WIM file.
300 * Disk information for the WIM file.
302 * Size, in bytes, of the new location information structure ('struct
303 * WimOverlay_dat_entry_2').
305 * Returns a pointer one past the last byte of @buf filled in.
308 fill_in_wimoverlay_dat(u8 *buf,
309 const struct WimOverlay_dat_header *old_hdr,
310 const wchar_t *wim_path,
311 const u8 wim_guid[WIM_GUID_LEN],
313 u64 new_data_source_id,
314 const PARTITION_INFORMATION_EX *part_info,
315 const DRIVE_LAYOUT_INFORMATION_EX *disk_info,
316 u32 new_entry_2_size)
318 struct WimOverlay_dat_header *new_hdr;
319 struct WimOverlay_dat_entry_1 *new_entry_1;
320 struct WimOverlay_dat_entry_2 *new_entry_2;
324 new_hdr = (struct WimOverlay_dat_header *)p;
326 /* Fill in new header */
327 new_hdr->magic = WIMOVERLAY_DAT_MAGIC;
328 new_hdr->wim_provider_version = WIM_PROVIDER_CURRENT_VERSION;
329 new_hdr->unknown_0x08 = 0x00000028;
330 new_hdr->num_entries_1 = (old_hdr ? old_hdr->num_entries_1 : 0) + 1;
331 new_hdr->num_entries_2 = (old_hdr ? old_hdr->num_entries_2 : 0) + 1;
332 new_hdr->unknown_0x14 = 0x00000000;
334 p += sizeof(struct WimOverlay_dat_header);
336 /* Copy WIM-specific information for old entries */
337 entry_2_offset = sizeof(struct WimOverlay_dat_header) +
338 (new_hdr->num_entries_1 * sizeof(struct WimOverlay_dat_entry_1));
340 for (u32 i = 0; i < old_hdr->num_entries_1; i++) {
341 new_entry_1 = (struct WimOverlay_dat_entry_1 *)p;
343 p = mempcpy(p, &old_hdr->entry_1s[i],
344 sizeof(struct WimOverlay_dat_entry_1));
346 new_entry_1->entry_2_offset = entry_2_offset;
347 entry_2_offset += new_entry_1->entry_2_length;
351 /* Generate WIM-specific information for new entry */
352 new_entry_1 = (struct WimOverlay_dat_entry_1 *)p;
354 new_entry_1->data_source_id = new_data_source_id;
355 new_entry_1->entry_2_offset = entry_2_offset;
356 new_entry_1->entry_2_length = new_entry_2_size;
357 new_entry_1->wim_type = WIM_BOOT_NOT_OS_WIM;
358 new_entry_1->wim_index = image;
359 BUILD_BUG_ON(sizeof(new_entry_1->guid) != WIM_GUID_LEN);
360 memcpy(new_entry_1->guid, wim_guid, WIM_GUID_LEN);
362 p += sizeof(struct WimOverlay_dat_entry_1);
364 /* Copy WIM location information for old entries */
366 for (u32 i = 0; i < old_hdr->num_entries_1; i++) {
367 wimlib_assert(new_hdr->entry_1s[i].entry_2_offset == p - buf);
368 wimlib_assert(old_hdr->entry_1s[i].entry_2_length ==
369 new_hdr->entry_1s[i].entry_2_length);
371 ((const u8 *)old_hdr + old_hdr->entry_1s[i].entry_2_offset),
372 old_hdr->entry_1s[i].entry_2_length);
376 /* Generate WIM location information for new entry */
377 new_entry_2 = (struct WimOverlay_dat_entry_2 *)p;
379 new_entry_2->unknown_0x00 = 0x00000000;
380 new_entry_2->unknown_0x04 = 0x00000000;
381 new_entry_2->entry_2_length = new_entry_2_size;
382 new_entry_2->unknown_0x0C = 0x00000000;
383 new_entry_2->unknown_0x10 = 0x00000005;
384 new_entry_2->unknown_0x14 = 0x00000001;
385 new_entry_2->inner_struct_size = new_entry_2_size - 0x14;
386 new_entry_2->unknown_0x1C = 0x00000005;
387 new_entry_2->unknown_0x20 = 0x00000006;
388 new_entry_2->unknown_0x24 = 0x00000000;
389 new_entry_2->unknown_0x28 = 0x00000048;
390 new_entry_2->unknown_0x2C = 0x00000000;
391 new_entry_2->unknown_0x40 = 0x00000000;
393 if (part_info->PartitionStyle == PARTITION_STYLE_MBR) {
394 new_entry_2->partition.mbr.part_start_offset = part_info->StartingOffset.QuadPart;
395 new_entry_2->partition.mbr.padding = 0;
396 new_entry_2->partition_table_type = WIMOVERLAY_PARTITION_TYPE_MBR;
397 new_entry_2->disk.mbr.disk_id = disk_info->Mbr.Signature;
398 new_entry_2->disk.mbr.padding[0] = 0x00000000;
399 new_entry_2->disk.mbr.padding[1] = 0x00000000;
400 new_entry_2->disk.mbr.padding[2] = 0x00000000;
402 BUILD_BUG_ON(sizeof(new_entry_2->partition.gpt.part_unique_guid) !=
403 sizeof(part_info->Gpt.PartitionId));
404 memcpy(new_entry_2->partition.gpt.part_unique_guid,
405 &part_info->Gpt.PartitionId,
406 sizeof(part_info->Gpt.PartitionId));
407 new_entry_2->partition_table_type = WIMOVERLAY_PARTITION_TYPE_GPT;
409 BUILD_BUG_ON(sizeof(new_entry_2->disk.gpt.disk_guid) !=
410 sizeof(disk_info->Gpt.DiskId));
411 memcpy(new_entry_2->disk.gpt.disk_guid,
412 &disk_info->Gpt.DiskId,
413 sizeof(disk_info->Gpt.DiskId));
415 BUILD_BUG_ON(sizeof(new_entry_2->disk.gpt.disk_guid) !=
416 sizeof(new_entry_2->partition.gpt.part_unique_guid));
418 new_entry_2->unknown_0x58[0] = 0x00000000;
419 new_entry_2->unknown_0x58[1] = 0x00000000;
420 new_entry_2->unknown_0x58[2] = 0x00000000;
421 new_entry_2->unknown_0x58[3] = 0x00000000;
423 wimlib_assert(wim_path[2] == L'\\');
424 return mempcpy(new_entry_2->wim_file_name,
426 new_entry_2_size - sizeof(struct WimOverlay_dat_entry_2));
430 * Prepares the new contents of WimOverlay.dat in memory, with one entry added.
433 * Old contents of WimOverlay.dat, or NULL if it did not exist.
435 * Absolute path to the WIM file. It must begin with a drive letter; for
436 * example, D:\install.wim.
438 * GUID of the WIM, from the WIM header.
440 * Number of the image in the WIM to specify in the new entry.
442 * Location into which to return the new contents as a malloc()ed buffer on
444 * @new_contents_size_ret
445 * Location into which to return the size, in bytes, of the new contents on
447 * @new_data_source_id_ret
448 * Location into which to return the data source ID of the new entry on
451 * Returns 0 on success, or a positive error code on failure.
454 prepare_wimoverlay_dat(const struct WimOverlay_dat_header *old_hdr,
455 const wchar_t *wim_path,
456 const u8 wim_guid[WIM_GUID_LEN],
458 void **new_contents_ret,
459 u32 *new_contents_size_ret,
460 u64 *new_data_source_id_ret)
463 PARTITION_INFORMATION_EX part_info;
464 DRIVE_LAYOUT_INFORMATION_EX disk_info;
465 u64 new_data_source_id;
466 u32 new_entry_2_size;
467 u32 new_contents_size;
471 ret = query_partition_and_disk_info(wim_path, &part_info, &disk_info);
475 new_data_source_id = alloc_new_data_source_id(old_hdr);
477 new_entry_2_size = sizeof(struct WimOverlay_dat_entry_2) +
478 ((wcslen(wim_path) - 2 + 1) * sizeof(wchar_t));
479 ret = calculate_wimoverlay_dat_size(old_hdr, new_entry_2_size,
484 buf = MALLOC(new_contents_size);
486 return WIMLIB_ERR_NOMEM;
488 end = fill_in_wimoverlay_dat(buf, old_hdr, wim_path, wim_guid, image,
490 &part_info, &disk_info, new_entry_2_size);
492 wimlib_assert(end - buf == new_contents_size);
494 *new_contents_ret = buf;
495 *new_contents_size_ret = new_contents_size;
496 *new_data_source_id_ret = new_data_source_id;
501 * Reads and validates a WimOverlay.dat file.
504 * Path to the WimOverlay.dat file, such as
505 * C:\System Volume Information\WimOverlay.dat
507 * Location into which to return the contents as a malloc()ed buffer on
508 * success. This can be cast to 'struct WimOverlay_dat_header', and its
509 * contents are guaranteed to be valid. Alternatively, if the file does
510 * not exist, NULL will be returned here.
512 * Returns 0 on success, or a positive error code on failure.
515 read_wimoverlay_dat(const wchar_t *path, void **contents_ret)
518 BY_HANDLE_FILE_INFORMATION info;
521 const struct WimOverlay_dat_header *hdr;
523 bool already_retried = false;
526 h = open_file(path, GENERIC_READ);
527 if (h == INVALID_HANDLE_VALUE) {
528 DWORD err = GetLastError();
529 if (err == ERROR_FILE_NOT_FOUND) {
530 *contents_ret = NULL;
533 if (err == ERROR_PATH_NOT_FOUND &&
534 func_RtlCreateSystemVolumeInformationFolder)
536 wchar_t volume_root_path[] = L"\\??\\X:\\";
538 *(wcschr(volume_root_path, L'X')) = path[0];
540 UNICODE_STRING str = {
541 .Length = sizeof(volume_root_path) - sizeof(wchar_t),
542 .MaximumLength = sizeof(volume_root_path),
543 .Buffer = volume_root_path,
548 status = (*func_RtlCreateSystemVolumeInformationFolder)(&str);
550 err2 = (*func_RtlNtStatusToDosError)(status);
551 if (err2 == ERROR_SUCCESS) {
552 if (!already_retried) {
553 already_retried = true;
560 set_errno_from_win32_error(err);
561 ERROR_WITH_ERRNO("\"%ls\": Can't open for reading", path);
562 return WIMLIB_ERR_OPEN;
564 if (!GetFileInformationByHandle(h, &info)) {
565 set_errno_from_GetLastError();
566 ERROR_WITH_ERRNO("\"%ls\": Can't query metadata", path);
568 return WIMLIB_ERR_STAT;
572 if (!info.nFileSizeHigh)
573 contents = MALLOC(info.nFileSizeLow);
575 ERROR("\"%ls\": File is too large to fit into memory", path);
577 return WIMLIB_ERR_NOMEM;
580 if (!ReadFile(h, contents, info.nFileSizeLow, &bytes_read, NULL) ||
581 bytes_read != info.nFileSizeLow)
583 set_errno_from_GetLastError();
584 ERROR_WITH_ERRNO("\"%ls\": Can't read data", path);
586 ret = WIMLIB_ERR_READ;
587 goto out_free_contents;
592 if (info.nFileSizeLow < sizeof(struct WimOverlay_dat_header)) {
593 ERROR("\"%ls\": File is unexpectedly small (only %"PRIu32" bytes)",
594 path, (u32)info.nFileSizeLow);
595 ret = WIMLIB_ERR_UNSUPPORTED;
596 goto out_free_contents;
599 hdr = (const struct WimOverlay_dat_header *)contents;
601 if (hdr->magic != WIMOVERLAY_DAT_MAGIC ||
602 hdr->wim_provider_version != WIM_PROVIDER_CURRENT_VERSION ||
603 hdr->unknown_0x08 != 0x00000028 ||
604 (hdr->num_entries_1 != hdr->num_entries_2) ||
605 hdr->unknown_0x14 != 0x00000000)
607 ERROR("\"%ls\": Header contains unexpected data:", path);
608 if (wimlib_print_errors) {
609 print_byte_field((const u8 *)hdr,
610 sizeof(struct WimOverlay_dat_header),
612 fputc('\n', wimlib_error_file);
614 ret = WIMLIB_ERR_UNSUPPORTED;
615 goto out_free_contents;
618 if ((u64)hdr->num_entries_1 * sizeof(struct WimOverlay_dat_entry_1) >
619 info.nFileSizeLow - sizeof(struct WimOverlay_dat_header))
621 ERROR("\"%ls\": File is unexpectedly small "
622 "(only %"PRIu32" bytes, but has %"PRIu32" entries)",
623 path, (u32)info.nFileSizeLow, hdr->num_entries_1);
624 ret = WIMLIB_ERR_UNSUPPORTED;
625 goto out_free_contents;
628 for (u32 i = 0; i < hdr->num_entries_1; i++) {
629 const struct WimOverlay_dat_entry_1 *entry_1;
630 const struct WimOverlay_dat_entry_2 *entry_2;
631 u32 wim_file_name_length;
633 entry_1 = &hdr->entry_1s[i];
635 if (((u64)entry_1->entry_2_offset +
636 (u64)entry_1->entry_2_length) >
639 ERROR("\"%ls\": entry %"PRIu32" (2) "
640 "(data source ID 0x%016"PRIx64") "
642 path, i, entry_1->data_source_id);
643 ret = WIMLIB_ERR_UNSUPPORTED;
644 goto out_free_contents;
646 if (entry_1->entry_2_length < sizeof(struct WimOverlay_dat_entry_2)) {
647 ERROR("\"%ls\": entry %"PRIu32" (2) "
648 "(data source ID 0x%016"PRIx64") "
650 path, i, entry_1->data_source_id);
651 ret = WIMLIB_ERR_UNSUPPORTED;
652 goto out_free_contents;
655 if (entry_1->entry_2_offset % 2 != 0) {
656 ERROR("\"%ls\": entry %"PRIu32" (2) "
657 "(data source ID 0x%016"PRIx64") "
659 path, i, entry_1->data_source_id);
660 ret = WIMLIB_ERR_UNSUPPORTED;
661 goto out_free_contents;
664 entry_2 = (const struct WimOverlay_dat_entry_2 *)
665 ((const u8 *)hdr + entry_1->entry_2_offset);
667 wim_file_name_length = entry_1->entry_2_length -
668 sizeof(struct WimOverlay_dat_entry_2);
669 if ((wim_file_name_length < 2 * sizeof(wchar_t)) ||
670 (wim_file_name_length % sizeof(wchar_t) != 0) ||
671 (wmemchr(entry_2->wim_file_name, L'\0',
672 wim_file_name_length / sizeof(wchar_t))
673 != &entry_2->wim_file_name[wim_file_name_length /
674 sizeof(wchar_t) - 1]))
676 ERROR("\"%ls\": entry %"PRIu32" (2) "
677 "(data source ID 0x%016"PRIx64") "
678 "has invalid WIM file name",
679 path, i, entry_1->data_source_id);
680 if (wimlib_print_errors) {
681 print_byte_field((const u8 *)entry_2->wim_file_name,
682 wim_file_name_length,
684 fputc('\n', wimlib_error_file);
686 ret = WIMLIB_ERR_UNSUPPORTED;
687 goto out_free_contents;
690 if (entry_2->unknown_0x00 != 0x00000000 ||
691 entry_2->unknown_0x04 != 0x00000000 ||
692 entry_2->unknown_0x0C != 0x00000000 ||
693 entry_2->entry_2_length != entry_1->entry_2_length ||
694 entry_2->unknown_0x10 != 0x00000005 ||
695 entry_2->unknown_0x14 != 0x00000001 ||
696 entry_2->inner_struct_size != entry_1->entry_2_length - 0x14 ||
697 entry_2->unknown_0x1C != 0x00000005 ||
698 entry_2->unknown_0x20 != 0x00000006 ||
699 entry_2->unknown_0x24 != 0x00000000 ||
700 entry_2->unknown_0x28 != 0x00000048 ||
701 entry_2->unknown_0x2C != 0x00000000 ||
702 entry_2->unknown_0x40 != 0x00000000 ||
703 (entry_2->partition_table_type != WIMOVERLAY_PARTITION_TYPE_GPT &&
704 entry_2->partition_table_type != WIMOVERLAY_PARTITION_TYPE_MBR) ||
705 (entry_2->partition_table_type == WIMOVERLAY_PARTITION_TYPE_MBR &&
706 entry_2->partition.mbr.padding != 0) ||
707 (entry_2->partition_table_type == WIMOVERLAY_PARTITION_TYPE_GPT &&
708 entry_2->partition.mbr.padding == 0) ||
709 entry_2->unknown_0x58[0] != 0x00000000 ||
710 entry_2->unknown_0x58[1] != 0x00000000 ||
711 entry_2->unknown_0x58[2] != 0x00000000 ||
712 entry_2->unknown_0x58[3] != 0x00000000)
714 ERROR("\"%ls\": entry %"PRIu32" (2) "
715 "(data source ID 0x%016"PRIx64") "
716 "contains unexpected data!",
717 path, i, entry_1->data_source_id);
718 if (wimlib_print_errors) {
719 print_byte_field((const u8 *)entry_2,
720 entry_1->entry_2_length,
722 fputc('\n', wimlib_error_file);
724 ret = WIMLIB_ERR_UNSUPPORTED;
725 goto out_free_contents;
729 *contents_ret = contents;
738 * Update WimOverlay.dat manually in order to add a WIM data source to the
741 * THIS CODE IS EXPERIMENTAL AS I HAD TO REVERSE ENGINEER THE FILE FORMAT!
744 * Target drive. Must be a letter followed by a colon (e.g. D:).
746 * Absolute path to the WIM file. It must begin with a drive letter; for
747 * example, D:\install.wim.
749 * GUID of the WIM, from the WIM header.
751 * Number of the image in the WIM to specify in the new entry.
752 * @data_source_id_ret
753 * On success, the allocated data source ID is returned here.
756 update_wimoverlay_manually(const wchar_t *drive, const wchar_t *wim_path,
757 const u8 wim_guid[WIM_GUID_LEN],
758 int image, u64 *data_source_id_ret)
760 wchar_t path_main[] = L"A:\\System Volume Information\\WimOverlay.dat";
761 wchar_t path_backup[] = L"A:\\System Volume Information\\WimOverlay.backup";
762 wchar_t path_wimlib_backup[] = L"A:\\System Volume Information\\WimOverlay.wimlib_backup";
763 wchar_t path_new[] = L"A:\\System Volume Information\\WimOverlay.wimlib_new";
766 u32 new_contents_size;
767 u64 new_data_source_id;
770 wimlib_assert(drive[0] != L'\0' &&
774 path_main[0] = drive[0];
775 path_backup[0] = drive[0];
776 path_wimlib_backup[0] = drive[0];
777 path_new[0] = drive[0];
779 ret = read_wimoverlay_dat(path_main, &old_contents);
783 ret = prepare_wimoverlay_dat(old_contents, wim_path, wim_guid, image,
784 &new_contents, &new_contents_size,
785 &new_data_source_id);
790 /* Write WimOverlay.wimlib_new */
791 ret = write_wimoverlay_dat(path_new,
792 new_contents, new_contents_size);
794 goto out_free_new_contents;
796 /* Write WimOverlay.backup */
797 ret = write_wimoverlay_dat(path_backup,
798 new_contents, new_contents_size);
800 goto out_free_new_contents;
803 /* Rename WimOverlay.dat => WimOverlay.wimlib_backup */
804 ret = win32_rename_replacement(path_main, path_wimlib_backup);
806 ERROR_WITH_ERRNO("Can't rename \"%ls\" => \"%ls\"",
807 path_main, path_wimlib_backup);
808 ret = WIMLIB_ERR_RENAME;
809 goto out_free_new_contents;
813 /* Rename WimOverlay.wimlib_new => WimOverlay.dat */
814 ret = win32_rename_replacement(path_new, path_main);
816 ERROR_WITH_ERRNO("Can't rename \"%ls\" => \"%ls\"",
817 path_new, path_main);
818 ret = WIMLIB_ERR_RENAME;
820 out_free_new_contents:
823 if (ret == WIMLIB_ERR_UNSUPPORTED) {
824 ERROR("Please report to developer ("PACKAGE_BUGREPORT").\n"
825 " If possible send the file \"%ls\".\n\n", path_main);
828 *data_source_id_ret = new_data_source_id;
832 /* Try to attach an instance of the Windows Overlay File System Filter Driver to
833 * the specified drive (such as C:) */
835 try_to_attach_wof(const wchar_t *drive)
840 /* Use FilterAttach() from Fltlib.dll. */
842 fltlib = LoadLibrary(L"Fltlib.dll");
845 WARNING("Failed to load Fltlib.dll");
849 HRESULT (WINAPI *func_FilterAttach)(LPCWSTR lpFilterName,
850 LPCWSTR lpVolumeName,
851 LPCWSTR lpInstanceName,
852 DWORD dwCreatedInstanceNameLength,
853 LPWSTR lpCreatedInstanceName);
855 func_FilterAttach = (void *)GetProcAddress(fltlib, "FilterAttach");
857 if (func_FilterAttach) {
860 res = (*func_FilterAttach)(L"WoF", drive, NULL, 0, NULL);
865 WARNING("FilterAttach() does not exist in Fltlib.dll");
874 * Allocate a WOF data source ID for a WIM file.
877 * Absolute path to the WIM file. This must include a drive letter and use
878 * backslash path separators.
880 * GUID of the WIM, from the WIM header.
882 * Number of the image in the WIM being applied.
884 * Path to the target directory.
885 * @data_source_id_ret
886 * On success, an identifier for the backing WIM file will be returned
889 * Returns 0 on success, or a positive error code on failure.
892 wimboot_alloc_data_source_id(const wchar_t *wim_path,
893 const u8 wim_guid[WIM_GUID_LEN],
894 int image, const wchar_t *target,
895 u64 *data_source_id_ret, bool *wof_running_ret)
898 size_t wim_path_nchars;
899 size_t wim_file_name_length;
902 struct wof_external_info *wof_info;
903 struct wim_provider_add_overlay_input *wim_info;
906 DWORD bytes_returned;
908 const wchar_t *prefix = L"\\??\\";
909 const size_t prefix_nchars = 4;
910 bool tried_to_attach_wof = false;
912 ret = win32_get_drive_path(target, drive_path);
916 wimlib_assert(!wcschr(wim_path, L'/'));
917 wimlib_assert(wim_path[0] != L'\0' && wim_path[1] == L':');
919 wim_path_nchars = wcslen(wim_path);
920 wim_file_name_length = sizeof(wchar_t) *
921 (wim_path_nchars + prefix_nchars);
923 insize = sizeof(struct wof_external_info) +
924 sizeof(struct wim_provider_add_overlay_input) +
925 wim_file_name_length;
929 ret = WIMLIB_ERR_NOMEM;
933 wof_info = (struct wof_external_info *)in;
934 wof_info->version = WOF_CURRENT_VERSION;
935 wof_info->provider = WOF_PROVIDER_WIM;
937 wim_info = (struct wim_provider_add_overlay_input *)(wof_info + 1);
938 wim_info->wim_type = WIM_BOOT_NOT_OS_WIM;
939 wim_info->wim_index = image;
940 wim_info->wim_file_name_offset = offsetof(struct wim_provider_add_overlay_input,
942 wim_info->wim_file_name_length = wim_file_name_length;
943 wmemcpy(&wim_info->wim_file_name[0], prefix, prefix_nchars);
944 wmemcpy(&wim_info->wim_file_name[prefix_nchars], wim_path, wim_path_nchars);
947 h = open_file(drive_path, GENERIC_WRITE);
949 if (h == INVALID_HANDLE_VALUE) {
950 set_errno_from_GetLastError();
951 ERROR_WITH_ERRNO("Failed to open \"%ls\"", drive_path + 4);
952 ret = WIMLIB_ERR_OPEN;
956 if (!DeviceIoControl(h, FSCTL_ADD_OVERLAY,
958 &data_source_id, sizeof(data_source_id),
959 &bytes_returned, NULL))
961 DWORD err = GetLastError();
962 if (err == ERROR_INVALID_FUNCTION) {
963 if (!tried_to_attach_wof) {
965 h = INVALID_HANDLE_VALUE;
966 tried_to_attach_wof = true;
967 if (try_to_attach_wof(drive_path + 4))
970 ret = WIMLIB_ERR_UNSUPPORTED;
971 goto out_close_handle;
973 set_errno_from_win32_error(err);
974 ERROR_WITH_ERRNO("Failed to add overlay source \"%ls\" "
975 "to volume \"%ls\" (err=0x%08"PRIx32")",
976 wim_path, drive_path + 4, (u32)err);
977 ret = WIMLIB_ERR_WIMBOOT;
978 goto out_close_handle;
982 if (bytes_returned != sizeof(data_source_id)) {
983 set_errno_from_win32_error(ERROR_INVALID_DATA);
984 ret = WIMLIB_ERR_WIMBOOT;
985 ERROR("Unexpected result size when adding "
986 "overlay source \"%ls\" to volume \"%ls\"",
987 wim_path, drive_path + 4);
988 goto out_close_handle;
991 *wof_running_ret = true;
992 *data_source_id_ret = data_source_id;
1000 if (ret == WIMLIB_ERR_UNSUPPORTED) {
1003 "The version of Windows you are running does not appear to support\n"
1004 " the Windows Overlay File System Filter Driver. This is normally\n"
1005 " available on Windows 8.1 Update 1 or later. Therefore, wimlib\n"
1006 " will attempt to update the WimOverlay.dat file directly.\n");
1008 WARNING("WOF driver is not available; updating WimOverlay.dat directly.");
1010 ret = update_wimoverlay_manually(drive_path + 4, wim_path,
1012 data_source_id_ret);
1013 *wof_running_ret = false;
1020 * Set WIMBoot information on the specified file.
1022 * This turns it into a reparse point that redirects accesses to it, to the
1023 * corresponding resource in the WIM archive.
1026 * Open handle to the file, with GENERIC_WRITE access.
1028 * Printable representation of the path to the file.
1030 * Unnamed data stream of the file.
1032 * Allocated identifier for the WIM data source on the destination volume.
1033 * @lookup_table_hash
1034 * SHA-1 message digest of the WIM's lookup table.
1036 * %true if the WOF driver appears to be available and working; %false if
1039 * Returns 0 on success, or a positive error code on failure.
1042 wimboot_set_pointer(HANDLE h,
1043 const wchar_t *printable_name,
1044 const struct wim_lookup_table_entry *lte,
1046 const u8 lookup_table_hash[SHA1_HASH_SIZE],
1049 DWORD bytes_returned;
1053 /* The WOF driver is running. We can create the reparse point
1054 * using FSCTL_SET_EXTERNAL_BACKING. */
1057 struct wof_external_info wof_info;
1058 struct wim_provider_external_info wim_info;
1061 in.wof_info.version = WOF_CURRENT_VERSION;
1062 in.wof_info.provider = WOF_PROVIDER_WIM;
1064 in.wim_info.version = WIM_PROVIDER_CURRENT_VERSION;
1065 in.wim_info.flags = 0;
1066 in.wim_info.data_source_id = data_source_id;
1067 copy_hash(in.wim_info.resource_hash, lte->hash);
1069 /* lookup_table_hash is not necessary */
1071 if (!DeviceIoControl(h, FSCTL_SET_EXTERNAL_BACKING,
1072 &in, sizeof(in), NULL, 0,
1073 &bytes_returned, NULL))
1077 /* The WOF driver is running. We need to create the reparse
1078 * point manually. */
1086 struct wof_external_info wof_info;
1087 struct wim_provider_rpdata wim_info;
1090 BUILD_BUG_ON(sizeof(in) != 8 +
1091 sizeof(struct wof_external_info) +
1092 sizeof(struct wim_provider_rpdata));
1094 in.hdr.rptag = WIMLIB_REPARSE_TAG_WOF;
1095 in.hdr.rpdatalen = sizeof(in) - sizeof(in.hdr);
1096 in.hdr.rpreserved = 0;
1098 in.wof_info.version = WOF_CURRENT_VERSION;
1099 in.wof_info.provider = WOF_PROVIDER_WIM;
1101 in.wim_info.version = 2;
1102 in.wim_info.flags = 0;
1103 in.wim_info.data_source_id = data_source_id;
1104 copy_hash(in.wim_info.resource_hash, lte->hash);
1105 copy_hash(in.wim_info.wim_lookup_table_hash, lookup_table_hash);
1106 in.wim_info.stream_uncompressed_size = lte->size;
1107 in.wim_info.stream_compressed_size = lte->rspec->size_in_wim;
1108 in.wim_info.stream_offset_in_wim = lte->rspec->offset_in_wim;
1110 if (!DeviceIoControl(h, FSCTL_SET_REPARSE_POINT,
1111 &in, sizeof(in), NULL, 0, &bytes_returned, NULL))
1114 /* We also need to create an unnamed data stream of the correct
1115 * size. Otherwise the file shows up as zero length. It can be
1116 * a sparse stream containing all zeroes; its contents
1117 * are unimportant. */
1118 if (!DeviceIoControl(h, FSCTL_SET_SPARSE, NULL, 0, NULL, 0,
1119 &bytes_returned, NULL))
1122 if (!SetFilePointerEx(h,
1123 (LARGE_INTEGER){ .QuadPart = lte->size},
1127 if (!SetEndOfFile(h))
1134 err = GetLastError();
1135 set_errno_from_win32_error(err);
1136 ERROR_WITH_ERRNO("\"%ls\": Couldn't set WIMBoot pointer data "
1137 "(err=%"PRIu32")", printable_name, (u32)err);
1138 return WIMLIB_ERR_WIMBOOT;
1141 #endif /* __WIN32__ */