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 part of wimlib, a library for working with WIM files.
18 * wimlib is free software; you can redistribute it and/or modify it under the
19 * terms of the GNU General Public License as published by the Free
20 * Software Foundation; either version 3 of the License, or (at your option)
23 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
24 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
25 * A PARTICULAR PURPOSE. See the GNU General Public License for more
28 * You should have received a copy of the GNU General Public License
29 * along with wimlib; if not, see http://www.gnu.org/licenses/.
39 #include "wimlib/win32_common.h"
40 #include "wimlib/win32.h"
41 #include "wimlib/assert.h"
42 #include "wimlib/error.h"
43 #include "wimlib/lookup_table.h"
44 #include "wimlib/util.h"
45 #include "wimlib/wimboot.h"
46 #include "wimlib/wof.h"
49 open_file(const wchar_t *device_name, DWORD desiredAccess)
51 return CreateFile(device_name, desiredAccess,
52 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
54 FILE_FLAG_BACKUP_SEMANTICS, NULL);
58 query_device(HANDLE h, DWORD code, void *out, DWORD out_size)
61 return DeviceIoControl(h, code, NULL, 0, out, out_size,
62 &bytes_returned, NULL);
66 * Gets partition and drive information for the specified path.
69 * Absolute path which must begin with a drive letter. For example, if the
70 * path is D:\install.wim, this function will query information about the
73 * Partition info is returned here.
75 * Drive info is returned here. The contained partition info will not be
78 * Returns 0 on success, or a positive error code on failure.
81 query_partition_and_disk_info(const wchar_t *path,
82 PARTITION_INFORMATION_EX *part_info_ret,
83 DRIVE_LAYOUT_INFORMATION_EX *drive_info_ret)
86 wchar_t vol_name[] = L"\\\\.\\X:";
87 wchar_t disk_name[] = L"\\\\?\\PhysicalDriveXXXXXXXXXX";
89 PARTITION_INFORMATION_EX part_info;
90 size_t extents_size = sizeof(VOLUME_DISK_EXTENTS) + 4 * sizeof(DISK_EXTENT);
91 VOLUME_DISK_EXTENTS *extents = alloca(extents_size);
92 size_t drive_info_size = sizeof(DRIVE_LAYOUT_INFORMATION_EX) +
93 8 * sizeof(PARTITION_INFORMATION_EX);
94 DRIVE_LAYOUT_INFORMATION_EX *drive_info = alloca(drive_info_size);
96 wimlib_assert(path[0] != L'\0' && path[1] == L':');
98 *(wcschr(vol_name, L'X')) = path[0];
100 h = open_file(vol_name, GENERIC_READ);
101 if (h == INVALID_HANDLE_VALUE) {
102 set_errno_from_GetLastError();
103 ERROR_WITH_ERRNO("\"%ls\": Can't open volume device", vol_name);
104 return WIMLIB_ERR_OPEN;
107 if (!query_device(h, IOCTL_DISK_GET_PARTITION_INFO_EX,
108 &part_info, sizeof(part_info)))
110 ERROR("\"%ls\": Can't get partition info (err=0x%08"PRIx32")",
111 vol_name, (u32)GetLastError());
113 return WIMLIB_ERR_READ;
116 if (!query_device(h, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
117 extents, extents_size))
119 ERROR("\"%ls\": Can't get volume extent info (err=0x%08"PRIx32")",
120 vol_name, (u32)GetLastError());
122 return WIMLIB_ERR_READ;
127 if (extents->NumberOfDiskExtents != 1) {
128 ERROR("\"%ls\": This volume has %"PRIu32" disk extents, "
129 "but this code is untested for more than 1",
130 vol_name, (u32)extents->NumberOfDiskExtents);
131 return WIMLIB_ERR_UNSUPPORTED;
134 wsprintf(wcschr(disk_name, L'X'), L"%"PRIu32,
135 extents->Extents[0].DiskNumber);
137 h = open_file(disk_name, GENERIC_READ);
138 if (h == INVALID_HANDLE_VALUE) {
139 set_errno_from_GetLastError();
140 ERROR_WITH_ERRNO("\"%ls\": Can't open disk device", disk_name);
141 return WIMLIB_ERR_OPEN;
144 if (!query_device(h, IOCTL_DISK_GET_DRIVE_LAYOUT_EX,
145 drive_info, drive_info_size))
147 ERROR("\"%ls\": Can't get disk info (err=0x%08"PRIx32")",
148 disk_name, (u32)GetLastError());
150 return WIMLIB_ERR_READ;
155 if (drive_info->PartitionStyle != part_info.PartitionStyle) {
156 ERROR("\"%ls\", \"%ls\": Inconsistent partition table type!",
157 vol_name, disk_name);
158 return WIMLIB_ERR_UNSUPPORTED;
161 if (part_info.PartitionStyle == PARTITION_STYLE_GPT) {
162 BUILD_BUG_ON(sizeof(part_info.Gpt.PartitionId) !=
163 sizeof(drive_info->Gpt.DiskId));
164 if (!memcmp(&part_info.Gpt.PartitionId,
165 &drive_info->Gpt.DiskId,
166 sizeof(drive_info->Gpt.DiskId)))
168 ERROR("\"%ls\", \"%ls\": Partition GUID is the "
169 "same as the disk GUID???", vol_name, disk_name);
170 return WIMLIB_ERR_UNSUPPORTED;
174 if (part_info.PartitionStyle != PARTITION_STYLE_MBR &&
175 part_info.PartitionStyle != PARTITION_STYLE_GPT)
177 ERROR("\"%ls\": Unknown partition style 0x%08"PRIx32,
178 vol_name, (u32)part_info.PartitionStyle);
179 return WIMLIB_ERR_UNSUPPORTED;
182 *part_info_ret = part_info;
183 *drive_info_ret = *drive_info;
188 * Allocate a new WIM data source ID.
191 * Previous WimOverlay.dat contents, or NULL if file did not exist.
193 * Returns the new data source ID.
196 alloc_new_data_source_id(const struct WimOverlay_dat_header *old_hdr)
201 for (u64 id = 0; ; id++) {
202 for (u32 i = 0; i < old_hdr->num_entries_1; i++)
203 if (id == old_hdr->entry_1s[i].data_source_id)
212 * Calculate the size of WimOverlay.dat with one entry added.
215 * Previous WimOverlay.dat contents, or NULL if file did not exist.
217 * Size of entry_2 being added.
219 * Size will be returned here.
221 * Returns 0 on success, or WIMLIB_ERR_UNSUPPORTED if size overflows 32 bits.
224 calculate_wimoverlay_dat_size(const struct WimOverlay_dat_header *old_hdr,
225 u32 new_entry_2_size, u32 *size_ret)
230 size_64 = sizeof(struct WimOverlay_dat_header);
232 for (u32 i = 0; i < old_hdr->num_entries_1; i++) {
233 size_64 += sizeof(struct WimOverlay_dat_entry_1);
234 size_64 += old_hdr->entry_1s[i].entry_2_length;
237 size_64 += sizeof(struct WimOverlay_dat_entry_1);
238 size_64 += new_entry_2_size;
242 return WIMLIB_ERR_UNSUPPORTED;
249 * Writes @size bytes of @contents to the named file @path.
251 * Returns 0 on success; WIMLIB_ERR_OPEN or WIMLIB_ERR_WRITE on failure.
254 write_wimoverlay_dat(const wchar_t *path, const void *contents, u32 size)
259 h = CreateFile(path, GENERIC_WRITE, 0, NULL,
260 CREATE_ALWAYS, FILE_FLAG_BACKUP_SEMANTICS, NULL);
261 if (h == INVALID_HANDLE_VALUE) {
262 set_errno_from_GetLastError();
263 ERROR_WITH_ERRNO("\"%ls\": Can't open file for writing", path);
264 return WIMLIB_ERR_OPEN;
267 if (!WriteFile(h, contents, size, &bytes_written, NULL) ||
268 bytes_written != size)
270 set_errno_from_GetLastError();
271 ERROR_WITH_ERRNO("\"%ls\": Can't write file", path);
273 return WIMLIB_ERR_WRITE;
276 if (!CloseHandle(h)) {
277 set_errno_from_GetLastError();
278 ERROR_WITH_ERRNO("\"%ls\": Can't close handle", path);
279 return WIMLIB_ERR_WRITE;
286 * Generates the contents of WimOverlay.dat in memory, with one entry added.
289 * Buffer large enough to hold the new contents.
291 * Old contents of WimOverlay.dat, or NULL if it did not exist.
293 * Absolute path to the WIM file. It must begin with a drive letter; for
294 * example, D:\install.wim.
296 * GUID of the WIM, from the WIM header.
298 * Number of the image in the WIM to specify in the new entry.
299 * @new_data_source_id
300 * Data source ID to use for the new entry.
302 * Partition information for the WIM file.
304 * Disk information for the WIM file.
306 * Size, in bytes, of the new location information structure ('struct
307 * WimOverlay_dat_entry_2').
309 * Returns a pointer one past the last byte of @buf filled in.
312 fill_in_wimoverlay_dat(u8 *buf,
313 const struct WimOverlay_dat_header *old_hdr,
314 const wchar_t *wim_path,
315 const u8 wim_guid[WIM_GID_LEN],
317 u64 new_data_source_id,
318 const PARTITION_INFORMATION_EX *part_info,
319 const DRIVE_LAYOUT_INFORMATION_EX *disk_info,
320 u32 new_entry_2_size)
322 struct WimOverlay_dat_header *new_hdr;
323 struct WimOverlay_dat_entry_1 *new_entry_1;
324 struct WimOverlay_dat_entry_2 *new_entry_2;
328 new_hdr = (struct WimOverlay_dat_header *)p;
330 /* Fill in new header */
331 new_hdr->magic = WIMOVERLAY_DAT_MAGIC;
332 new_hdr->wim_provider_version = WIM_PROVIDER_CURRENT_VERSION;
333 new_hdr->unknown_0x08 = 0x00000028;
334 new_hdr->num_entries_1 = (old_hdr ? old_hdr->num_entries_1 : 0) + 1;
335 new_hdr->num_entries_2 = (old_hdr ? old_hdr->num_entries_2 : 0) + 1;
336 new_hdr->unknown_0x14 = 0x00000000;
338 p += sizeof(struct WimOverlay_dat_header);
340 /* Copy WIM-specific information for old entries */
341 entry_2_offset = sizeof(struct WimOverlay_dat_header) +
342 (new_hdr->num_entries_1 * sizeof(struct WimOverlay_dat_entry_1));
344 for (u32 i = 0; i < old_hdr->num_entries_1; i++) {
345 new_entry_1 = (struct WimOverlay_dat_entry_1 *)p;
347 p = mempcpy(p, &old_hdr->entry_1s[i],
348 sizeof(struct WimOverlay_dat_entry_1));
350 new_entry_1->entry_2_offset = entry_2_offset;
351 entry_2_offset += new_entry_1->entry_2_length;
355 /* Generate WIM-specific information for new entry */
356 new_entry_1 = (struct WimOverlay_dat_entry_1 *)p;
358 new_entry_1->data_source_id = new_data_source_id;
359 new_entry_1->entry_2_offset = entry_2_offset;
360 new_entry_1->entry_2_length = new_entry_2_size;
361 new_entry_1->wim_type = WIM_BOOT_NOT_OS_WIM;
362 new_entry_1->wim_index = image;
363 BUILD_BUG_ON(sizeof(new_entry_1->guid) != WIM_GID_LEN);
364 memcpy(new_entry_1->guid, wim_guid, WIM_GID_LEN);
366 p += sizeof(struct WimOverlay_dat_entry_1);
368 /* Copy WIM location information for old entries */
370 for (u32 i = 0; i < old_hdr->num_entries_1; i++) {
371 wimlib_assert(new_hdr->entry_1s[i].entry_2_offset == p - buf);
372 wimlib_assert(old_hdr->entry_1s[i].entry_2_length ==
373 new_hdr->entry_1s[i].entry_2_length);
375 ((const u8 *)old_hdr + old_hdr->entry_1s[i].entry_2_offset),
376 old_hdr->entry_1s[i].entry_2_length);
380 /* Generate WIM location information for new entry */
381 new_entry_2 = (struct WimOverlay_dat_entry_2 *)p;
383 new_entry_2->unknown_0x00 = 0x00000000;
384 new_entry_2->unknown_0x04 = 0x00000000;
385 new_entry_2->entry_2_length = new_entry_2_size;
386 new_entry_2->unknown_0x0C = 0x00000000;
387 new_entry_2->unknown_0x10 = 0x00000005;
388 new_entry_2->unknown_0x14 = 0x00000001;
389 new_entry_2->inner_struct_size = new_entry_2_size - 0x14;
390 new_entry_2->unknown_0x1C = 0x00000005;
391 new_entry_2->unknown_0x20 = 0x00000006;
392 new_entry_2->unknown_0x24 = 0x00000000;
393 new_entry_2->unknown_0x28 = 0x00000048;
394 new_entry_2->unknown_0x2C = 0x00000000;
395 new_entry_2->unknown_0x40 = 0x00000000;
397 if (part_info->PartitionStyle == PARTITION_STYLE_MBR) {
398 new_entry_2->partition.mbr.part_start_offset = part_info->StartingOffset.QuadPart;
399 new_entry_2->partition.mbr.padding = 0;
400 new_entry_2->partition_table_type = WIMOVERLAY_PARTITION_TYPE_MBR;
401 new_entry_2->disk.mbr.disk_id = disk_info->Mbr.Signature;
402 new_entry_2->disk.mbr.padding[0] = 0x00000000;
403 new_entry_2->disk.mbr.padding[1] = 0x00000000;
404 new_entry_2->disk.mbr.padding[2] = 0x00000000;
406 BUILD_BUG_ON(sizeof(new_entry_2->partition.gpt.part_unique_guid) !=
407 sizeof(part_info->Gpt.PartitionId));
408 memcpy(new_entry_2->partition.gpt.part_unique_guid,
409 &part_info->Gpt.PartitionId,
410 sizeof(part_info->Gpt.PartitionId));
411 new_entry_2->partition_table_type = WIMOVERLAY_PARTITION_TYPE_GPT;
413 BUILD_BUG_ON(sizeof(new_entry_2->disk.gpt.disk_guid) !=
414 sizeof(disk_info->Gpt.DiskId));
415 memcpy(new_entry_2->disk.gpt.disk_guid,
416 &disk_info->Gpt.DiskId,
417 sizeof(disk_info->Gpt.DiskId));
419 BUILD_BUG_ON(sizeof(new_entry_2->disk.gpt.disk_guid) !=
420 sizeof(new_entry_2->partition.gpt.part_unique_guid));
422 new_entry_2->unknown_0x58[0] = 0x00000000;
423 new_entry_2->unknown_0x58[1] = 0x00000000;
424 new_entry_2->unknown_0x58[2] = 0x00000000;
425 new_entry_2->unknown_0x58[3] = 0x00000000;
427 wimlib_assert(wim_path[2] == L'\\');
428 return mempcpy(new_entry_2->wim_file_name,
430 new_entry_2_size - sizeof(struct WimOverlay_dat_entry_2));
434 * Prepares the new contents of WimOverlay.dat in memory, with one entry added.
437 * Old contents of WimOverlay.dat, or NULL if it did not exist.
439 * Absolute path to the WIM file. It must begin with a drive letter; for
440 * example, D:\install.wim.
442 * GUID of the WIM, from the WIM header.
444 * Number of the image in the WIM to specify in the new entry.
446 * Location into which to return the new contents as a malloc()ed buffer on
448 * @new_contents_size_ret
449 * Location into which to return the size, in bytes, of the new contents on
451 * @new_data_source_id_ret
452 * Location into which to return the data source ID of the new entry on
455 * Returns 0 on success, or a positive error code on failure.
458 prepare_wimoverlay_dat(const struct WimOverlay_dat_header *old_hdr,
459 const wchar_t *wim_path,
460 const u8 wim_guid[WIM_GID_LEN],
462 void **new_contents_ret,
463 u32 *new_contents_size_ret,
464 u64 *new_data_source_id_ret)
467 PARTITION_INFORMATION_EX part_info;
468 DRIVE_LAYOUT_INFORMATION_EX disk_info;
469 u64 new_data_source_id;
470 u32 new_entry_2_size;
471 u32 new_contents_size;
475 ret = query_partition_and_disk_info(wim_path, &part_info, &disk_info);
479 new_data_source_id = alloc_new_data_source_id(old_hdr);
481 new_entry_2_size = sizeof(struct WimOverlay_dat_entry_2) +
482 ((wcslen(wim_path) - 2 + 1) * sizeof(wchar_t));
483 ret = calculate_wimoverlay_dat_size(old_hdr, new_entry_2_size,
488 buf = MALLOC(new_contents_size);
490 return WIMLIB_ERR_NOMEM;
492 end = fill_in_wimoverlay_dat(buf, old_hdr, wim_path, wim_guid, image,
494 &part_info, &disk_info, new_entry_2_size);
496 wimlib_assert(end - buf == new_contents_size);
498 *new_contents_ret = buf;
499 *new_contents_size_ret = new_contents_size;
500 *new_data_source_id_ret = new_data_source_id;
505 * Reads and validates a WimOverlay.dat file.
508 * Path to the WimOverlay.dat file, such as
509 * C:\System Volume Information\WimOverlay.dat
511 * Location into which to return the contents as a malloc()ed buffer on
512 * success. This can be cast to 'struct WimOverlay_dat_header', and its
513 * contents are guaranteed to be valid. Alternatively, if the file does
514 * not exist, NULL will be returned here.
516 * Returns 0 on success, or a positive error code on failure.
519 read_wimoverlay_dat(const wchar_t *path, void **contents_ret)
522 BY_HANDLE_FILE_INFORMATION info;
525 const struct WimOverlay_dat_header *hdr;
527 bool already_retried = false;
530 h = open_file(path, GENERIC_READ);
531 if (h == INVALID_HANDLE_VALUE) {
532 DWORD err = GetLastError();
533 if (err == ERROR_FILE_NOT_FOUND) {
534 *contents_ret = NULL;
537 if (err == ERROR_PATH_NOT_FOUND &&
538 func_RtlCreateSystemVolumeInformationFolder)
540 wchar_t volume_root_path[] = L"\\??\\X:\\";
542 *(wcschr(volume_root_path, L'X')) = path[0];
544 UNICODE_STRING str = {
545 .Length = sizeof(volume_root_path) - sizeof(wchar_t),
546 .MaximumLength = sizeof(volume_root_path),
547 .Buffer = volume_root_path,
552 status = (*func_RtlCreateSystemVolumeInformationFolder)(&str);
554 err2 = (*func_RtlNtStatusToDosError)(status);
555 if (err2 == ERROR_SUCCESS) {
556 if (!already_retried) {
557 already_retried = true;
564 set_errno_from_win32_error(err);
565 ERROR_WITH_ERRNO("\"%ls\": Can't open for reading", path);
566 return WIMLIB_ERR_OPEN;
568 if (!GetFileInformationByHandle(h, &info)) {
569 set_errno_from_GetLastError();
570 ERROR_WITH_ERRNO("\"%ls\": Can't query metadata", path);
572 return WIMLIB_ERR_STAT;
576 if (!info.nFileSizeHigh)
577 contents = MALLOC(info.nFileSizeLow);
579 ERROR("\"%ls\": File is too large to fit into memory", path);
581 return WIMLIB_ERR_NOMEM;
584 if (!ReadFile(h, contents, info.nFileSizeLow, &bytes_read, NULL) ||
585 bytes_read != info.nFileSizeLow)
587 set_errno_from_GetLastError();
588 ERROR_WITH_ERRNO("\"%ls\": Can't read data", path);
590 ret = WIMLIB_ERR_READ;
591 goto out_free_contents;
596 if (info.nFileSizeLow < sizeof(struct WimOverlay_dat_header)) {
597 ERROR("\"%ls\": File is unexpectedly small (only %"PRIu32" bytes)",
598 path, (u32)info.nFileSizeLow);
599 ret = WIMLIB_ERR_UNSUPPORTED;
600 goto out_free_contents;
603 hdr = (const struct WimOverlay_dat_header *)contents;
605 if (hdr->magic != WIMOVERLAY_DAT_MAGIC ||
606 hdr->wim_provider_version != WIM_PROVIDER_CURRENT_VERSION ||
607 hdr->unknown_0x08 != 0x00000028 ||
608 (hdr->num_entries_1 != hdr->num_entries_2) ||
609 hdr->unknown_0x14 != 0x00000000)
611 ERROR("\"%ls\": Header contains unexpected data:", path);
612 if (wimlib_print_errors) {
613 print_byte_field((const u8 *)hdr,
614 sizeof(struct WimOverlay_dat_header),
618 ret = WIMLIB_ERR_UNSUPPORTED;
619 goto out_free_contents;
622 if ((u64)hdr->num_entries_1 * sizeof(struct WimOverlay_dat_entry_1) >
623 info.nFileSizeLow - sizeof(struct WimOverlay_dat_header))
625 ERROR("\"%ls\": File is unexpectedly small "
626 "(only %"PRIu32" bytes, but has %"PRIu32" entries)",
627 path, (u32)info.nFileSizeLow, hdr->num_entries_1);
628 ret = WIMLIB_ERR_UNSUPPORTED;
629 goto out_free_contents;
632 for (u32 i = 0; i < hdr->num_entries_1; i++) {
633 const struct WimOverlay_dat_entry_1 *entry_1;
634 const struct WimOverlay_dat_entry_2 *entry_2;
635 u32 wim_file_name_length;
637 entry_1 = &hdr->entry_1s[i];
639 if (((u64)entry_1->entry_2_offset +
640 (u64)entry_1->entry_2_length) >
643 ERROR("\"%ls\": entry %"PRIu32" (2) "
644 "(data source ID 0x%016"PRIx64") "
646 path, i, entry_1->data_source_id);
647 ret = WIMLIB_ERR_UNSUPPORTED;
648 goto out_free_contents;
650 if (entry_1->entry_2_length < sizeof(struct WimOverlay_dat_entry_2)) {
651 ERROR("\"%ls\": entry %"PRIu32" (2) "
652 "(data source ID 0x%016"PRIx64") "
654 path, i, entry_1->data_source_id);
655 ret = WIMLIB_ERR_UNSUPPORTED;
656 goto out_free_contents;
659 if (entry_1->entry_2_offset % 2 != 0) {
660 ERROR("\"%ls\": entry %"PRIu32" (2) "
661 "(data source ID 0x%016"PRIx64") "
663 path, i, entry_1->data_source_id);
664 ret = WIMLIB_ERR_UNSUPPORTED;
665 goto out_free_contents;
668 entry_2 = (const struct WimOverlay_dat_entry_2 *)
669 ((const u8 *)hdr + entry_1->entry_2_offset);
671 wim_file_name_length = entry_1->entry_2_length -
672 sizeof(struct WimOverlay_dat_entry_2);
673 if ((wim_file_name_length < 2 * sizeof(wchar_t)) ||
674 (wim_file_name_length % sizeof(wchar_t) != 0) ||
675 (wmemchr(entry_2->wim_file_name, L'\0',
676 wim_file_name_length / sizeof(wchar_t))
677 != &entry_2->wim_file_name[wim_file_name_length /
678 sizeof(wchar_t) - 1]))
680 ERROR("\"%ls\": entry %"PRIu32" (2) "
681 "(data source ID 0x%016"PRIx64") "
682 "has invalid WIM file name",
683 path, i, entry_1->data_source_id);
684 if (wimlib_print_errors) {
685 print_byte_field((const u8 *)entry_2->wim_file_name,
686 wim_file_name_length, stderr);
689 ret = WIMLIB_ERR_UNSUPPORTED;
690 goto out_free_contents;
693 if (entry_2->unknown_0x00 != 0x00000000 ||
694 entry_2->unknown_0x04 != 0x00000000 ||
695 entry_2->unknown_0x0C != 0x00000000 ||
696 entry_2->entry_2_length != entry_1->entry_2_length ||
697 entry_2->unknown_0x10 != 0x00000005 ||
698 entry_2->unknown_0x14 != 0x00000001 ||
699 entry_2->inner_struct_size != entry_1->entry_2_length - 0x14 ||
700 entry_2->unknown_0x1C != 0x00000005 ||
701 entry_2->unknown_0x20 != 0x00000006 ||
702 entry_2->unknown_0x24 != 0x00000000 ||
703 entry_2->unknown_0x28 != 0x00000048 ||
704 entry_2->unknown_0x2C != 0x00000000 ||
705 entry_2->unknown_0x40 != 0x00000000 ||
706 (entry_2->partition_table_type != WIMOVERLAY_PARTITION_TYPE_GPT &&
707 entry_2->partition_table_type != WIMOVERLAY_PARTITION_TYPE_MBR) ||
708 (entry_2->partition_table_type == WIMOVERLAY_PARTITION_TYPE_MBR &&
709 entry_2->partition.mbr.padding != 0) ||
710 (entry_2->partition_table_type == WIMOVERLAY_PARTITION_TYPE_GPT &&
711 entry_2->partition.mbr.padding == 0) ||
712 entry_2->unknown_0x58[0] != 0x00000000 ||
713 entry_2->unknown_0x58[1] != 0x00000000 ||
714 entry_2->unknown_0x58[2] != 0x00000000 ||
715 entry_2->unknown_0x58[3] != 0x00000000)
717 ERROR("\"%ls\": entry %"PRIu32" (2) "
718 "(data source ID 0x%016"PRIx64") "
719 "contains unexpected data!",
720 path, i, entry_1->data_source_id);
721 if (wimlib_print_errors) {
722 print_byte_field((const u8 *)entry_2,
723 entry_1->entry_2_length,
727 ret = WIMLIB_ERR_UNSUPPORTED;
728 goto out_free_contents;
732 *contents_ret = contents;
741 * Update WimOverlay.dat manually in order to add a WIM data source to the
744 * THIS CODE IS EXPERIMENTAL AS I HAD TO REVERSE ENGINEER THE FILE FORMAT!
747 * Target drive. Must be a letter followed by a colon (e.g. D:).
749 * Absolute path to the WIM file. It must begin with a drive letter; for
750 * example, D:\install.wim.
752 * GUID of the WIM, from the WIM header.
754 * Number of the image in the WIM to specify in the new entry.
755 * @data_source_id_ret
756 * On success, the allocated data source ID is returned here.
759 update_wimoverlay_manually(const wchar_t *drive, const wchar_t *wim_path,
760 const u8 wim_guid[WIM_GID_LEN],
761 int image, u64 *data_source_id_ret)
763 wchar_t path_main[] = L"A:\\System Volume Information\\WimOverlay.dat";
764 wchar_t path_backup[] = L"A:\\System Volume Information\\WimOverlay.backup";
765 wchar_t path_wimlib_backup[] = L"A:\\System Volume Information\\WimOverlay.wimlib_backup";
766 wchar_t path_new[] = L"A:\\System Volume Information\\WimOverlay.wimlib_new";
769 u32 new_contents_size;
770 u64 new_data_source_id;
773 wimlib_assert(drive[0] != L'\0' &&
777 path_main[0] = drive[0];
778 path_backup[0] = drive[0];
779 path_wimlib_backup[0] = drive[0];
780 path_new[0] = drive[0];
782 ret = read_wimoverlay_dat(path_main, &old_contents);
786 ret = prepare_wimoverlay_dat(old_contents, wim_path, wim_guid, image,
787 &new_contents, &new_contents_size,
788 &new_data_source_id);
793 /* Write WimOverlay.wimlib_new */
794 ret = write_wimoverlay_dat(path_new,
795 new_contents, new_contents_size);
797 goto out_free_new_contents;
799 /* Write WimOverlay.backup */
800 ret = write_wimoverlay_dat(path_backup,
801 new_contents, new_contents_size);
803 goto out_free_new_contents;
806 /* Rename WimOverlay.dat => WimOverlay.wimlib_backup */
807 ret = win32_rename_replacement(path_main, path_wimlib_backup);
809 ERROR_WITH_ERRNO("Can't rename \"%ls\" => \"%ls\"",
810 path_main, path_wimlib_backup);
811 goto out_free_new_contents;
815 /* Rename WimOverlay.wimlib_new => WimOverlay.dat */
816 ret = win32_rename_replacement(path_new, path_main);
818 ERROR_WITH_ERRNO("Can't rename \"%ls\" => \"%ls\"",
819 path_new, path_main);
821 out_free_new_contents:
824 if (ret == WIMLIB_ERR_UNSUPPORTED) {
825 ERROR("Please report to developer ("PACKAGE_BUGREPORT").\n"
826 " If possible send the file \"%ls\".\n\n", path_main);
829 *data_source_id_ret = new_data_source_id;
834 win32_get_drive_path(const wchar_t *file_path, wchar_t drive_path[7])
838 file_abspath = realpath(file_path, NULL);
840 return WIMLIB_ERR_NOMEM;
842 if (file_abspath[0] == L'\0' || file_abspath[1] != L':') {
843 ERROR("\"%ls\": Path format not recognized", file_abspath);
845 return WIMLIB_ERR_UNSUPPORTED;
848 wsprintf(drive_path, L"\\\\.\\%lc:", file_abspath[0]);
853 /* Try to attach an instance of the Windows Overlay File System Filter Driver to
854 * the specified drive (such as C:) */
856 try_to_attach_wof(const wchar_t *drive)
861 /* Use FilterAttach() from Fltlib.dll. */
863 fltlib = LoadLibrary(L"Fltlib.dll");
866 WARNING("Failed to load Fltlib.dll");
870 HRESULT (WINAPI *func_FilterAttach)(LPCWSTR lpFilterName,
871 LPCWSTR lpVolumeName,
872 LPCWSTR lpInstanceName,
873 DWORD dwCreatedInstanceNameLength,
874 LPWSTR lpCreatedInstanceName);
876 func_FilterAttach = (void *)GetProcAddress(fltlib, "FilterAttach");
878 if (func_FilterAttach) {
881 res = (*func_FilterAttach)(L"WoF", drive, NULL, 0, NULL);
886 WARNING("FilterAttach() does not exist in Fltlib.dll");
895 * Allocate a WOF data source ID for a WIM file.
898 * Absolute path to the WIM file. This must include a drive letter and use
899 * backslash path separators.
901 * GUID of the WIM, from the WIM header.
903 * Number of the image in the WIM being applied.
905 * Path to the target drive.
906 * @data_source_id_ret
907 * On success, an identifier for the backing WIM file will be returned
910 * Returns 0 on success, or a positive error code on failure.
913 wimboot_alloc_data_source_id(const wchar_t *wim_path,
914 const u8 wim_guid[WIM_GID_LEN],
915 int image, const wchar_t *target,
916 u64 *data_source_id_ret, bool *wof_running_ret)
919 size_t wim_path_nchars;
920 size_t wim_file_name_length;
923 struct wof_external_info *wof_info;
924 struct wim_provider_add_overlay_input *wim_info;
927 DWORD bytes_returned;
929 const wchar_t *prefix = L"\\??\\";
930 const size_t prefix_nchars = 4;
931 bool tried_to_attach_wof = false;
933 ret = win32_get_drive_path(target, drive_path);
937 wimlib_assert(!wcschr(wim_path, L'/'));
938 wimlib_assert(wim_path[0] != L'\0' && wim_path[1] == L':');
940 wim_path_nchars = wcslen(wim_path);
941 wim_file_name_length = sizeof(wchar_t) *
942 (wim_path_nchars + prefix_nchars);
944 insize = sizeof(struct wof_external_info) +
945 sizeof(struct wim_provider_add_overlay_input) +
946 wim_file_name_length;
950 ret = WIMLIB_ERR_NOMEM;
954 wof_info = (struct wof_external_info *)in;
955 wof_info->version = WOF_CURRENT_VERSION;
956 wof_info->provider = WOF_PROVIDER_WIM;
958 wim_info = (struct wim_provider_add_overlay_input *)(wof_info + 1);
959 wim_info->wim_type = WIM_BOOT_NOT_OS_WIM;
960 wim_info->wim_index = image;
961 wim_info->wim_file_name_offset = offsetof(struct wim_provider_add_overlay_input,
963 wim_info->wim_file_name_length = wim_file_name_length;
964 wmemcpy(&wim_info->wim_file_name[0], prefix, prefix_nchars);
965 wmemcpy(&wim_info->wim_file_name[prefix_nchars], wim_path, wim_path_nchars);
968 h = open_file(drive_path, GENERIC_WRITE);
970 if (h == INVALID_HANDLE_VALUE) {
971 set_errno_from_GetLastError();
972 ERROR_WITH_ERRNO("Failed to open \"%ls\"", drive_path + 4);
973 ret = WIMLIB_ERR_OPEN;
977 if (!DeviceIoControl(h, FSCTL_ADD_OVERLAY,
979 &data_source_id, sizeof(data_source_id),
980 &bytes_returned, NULL))
982 DWORD err = GetLastError();
983 if (err == ERROR_INVALID_FUNCTION) {
984 if (!tried_to_attach_wof) {
986 h = INVALID_HANDLE_VALUE;
987 tried_to_attach_wof = true;
988 if (try_to_attach_wof(drive_path + 4))
991 ret = WIMLIB_ERR_UNSUPPORTED;
992 goto out_close_handle;
994 set_errno_from_win32_error(err);
995 ERROR_WITH_ERRNO("Failed to add overlay source \"%ls\" "
996 "to volume \"%ls\" (err=0x%08"PRIx32")",
997 wim_path, drive_path + 4, (u32)err);
998 ret = WIMLIB_ERR_WIMBOOT;
999 goto out_close_handle;
1003 if (bytes_returned != sizeof(data_source_id)) {
1004 set_errno_from_win32_error(ERROR_INVALID_DATA);
1005 ret = WIMLIB_ERR_WIMBOOT;
1006 ERROR("Unexpected result size when adding "
1007 "overlay source \"%ls\" to volume \"%ls\"",
1008 wim_path, drive_path + 4);
1009 goto out_close_handle;
1012 *wof_running_ret = true;
1013 *data_source_id_ret = data_source_id;
1021 if (ret == WIMLIB_ERR_UNSUPPORTED) {
1024 "The version of Windows you are running does not appear to support\n"
1025 " the Windows Overlay File System Filter Driver. This is normally\n"
1026 " available on Windows 8.1 Update 1 or later. Therefore, wimlib\n"
1027 " will attempt to update the WimOverlay.dat file directly.\n");
1029 WARNING("WOF driver is not available; updating WimOverlay.dat directly.");
1031 ret = update_wimoverlay_manually(drive_path + 4, wim_path,
1033 data_source_id_ret);
1034 *wof_running_ret = false;
1041 * Set WIMBoot information on the specified file.
1043 * This turns it into a reparse point that redirects accesses to it, to the
1044 * corresponding resource in the WIM archive.
1047 * Path to extracted file (already created).
1049 * Unnamed data stream of the file.
1051 * Allocated identifier for the WIM data source on the destination volume.
1052 * @lookup_table_hash
1053 * SHA-1 message digest of the WIM's lookup table.
1055 * %true if the WOF driver appears to be available and working; %false if
1058 * Returns 0 on success, or a positive error code on failure.
1061 wimboot_set_pointer(const wchar_t *path,
1062 const struct wim_lookup_table_entry *lte,
1064 const u8 lookup_table_hash[SHA1_HASH_SIZE],
1068 DWORD bytes_returned;
1073 h = win32_open_existing_file(path, GENERIC_WRITE);
1074 if (h == INVALID_HANDLE_VALUE) {
1075 set_errno_from_GetLastError();
1076 ret = WIMLIB_ERR_OPEN;
1081 /* The WOF driver is running. We can create the reparse point
1082 * using FSCTL_SET_EXTERNAL_BACKING. */
1085 struct wof_external_info wof_info;
1086 struct wim_provider_external_info wim_info;
1089 in.wof_info.version = WOF_CURRENT_VERSION;
1090 in.wof_info.provider = WOF_PROVIDER_WIM;
1092 in.wim_info.version = WIM_PROVIDER_CURRENT_VERSION;
1093 in.wim_info.flags = 0;
1094 in.wim_info.data_source_id = data_source_id;
1095 copy_hash(in.wim_info.resource_hash, lte->hash);
1097 /* lookup_table_hash is not necessary */
1099 if (!DeviceIoControl(h, FSCTL_SET_EXTERNAL_BACKING,
1100 &in, sizeof(in), NULL, 0, &bytes_returned, NULL))
1104 /* The WOF driver is running. We need to create the reparse
1105 * point manually. */
1113 struct wof_external_info wof_info;
1114 struct wim_provider_rpdata wim_info;
1117 BUILD_BUG_ON(sizeof(in) != 8 +
1118 sizeof(struct wof_external_info) +
1119 sizeof(struct wim_provider_rpdata));
1121 in.hdr.rptag = WIMLIB_REPARSE_TAG_WOF;
1122 in.hdr.rpdatalen = sizeof(in) - sizeof(in.hdr);
1123 in.hdr.rpreserved = 0;
1125 in.wof_info.version = WOF_CURRENT_VERSION;
1126 in.wof_info.provider = WOF_PROVIDER_WIM;
1128 in.wim_info.version = 2;
1129 in.wim_info.flags = 0;
1130 in.wim_info.data_source_id = data_source_id;
1131 copy_hash(in.wim_info.resource_hash, lte->hash);
1132 copy_hash(in.wim_info.wim_lookup_table_hash, lookup_table_hash);
1133 in.wim_info.stream_uncompressed_size = lte->size;
1134 in.wim_info.stream_compressed_size = lte->rspec->size_in_wim;
1135 in.wim_info.stream_offset_in_wim = lte->rspec->offset_in_wim;
1137 if (!DeviceIoControl(h, FSCTL_SET_REPARSE_POINT,
1138 &in, sizeof(in), NULL, 0, &bytes_returned, NULL))
1141 /* We also need to create an unnamed data stream of the correct
1142 * size. Otherwise the file shows up as zero length. It can be
1143 * a sparse stream containing all zeroes; its contents
1144 * are unimportant. */
1145 if (!DeviceIoControl(h, FSCTL_SET_SPARSE, NULL, 0, NULL, 0,
1146 &bytes_returned, NULL))
1149 if (!SetFilePointerEx(h,
1150 (LARGE_INTEGER){ .QuadPart = lte->size},
1154 if (!SetEndOfFile(h))
1166 DWORD err = GetLastError();
1167 set_errno_from_win32_error(err);
1168 ERROR_WITH_ERRNO("\"%ls\": Couldn't set WIMBoot pointer data "
1169 "(err=0x%08"PRIx32")", path, (u32)err);
1170 ret = WIMLIB_ERR_WIMBOOT;
1171 goto out_close_handle;
1175 #endif /* __WIN32__ */