2013-01-19 04:04:54 +00:00
|
|
|
/*
|
|
|
|
* Rufus: The Reliable USB Formatting Utility
|
|
|
|
* Virtual Disk Handling functions
|
2016-01-14 17:43:02 +00:00
|
|
|
* Copyright © 2013-2016 Pete Batard <pete@akeo.ie>
|
2013-01-19 04:04:54 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2013-01-20 22:46:11 +00:00
|
|
|
#include <windows.h>
|
2014-05-22 00:52:25 +00:00
|
|
|
#include <stdlib.h>
|
2013-01-20 22:46:11 +00:00
|
|
|
#include <io.h>
|
2014-05-23 00:03:01 +00:00
|
|
|
#include <rpc.h>
|
|
|
|
#include <time.h>
|
2013-01-20 22:46:11 +00:00
|
|
|
|
2013-01-19 04:04:54 +00:00
|
|
|
#include "rufus.h"
|
2016-02-20 22:52:32 +00:00
|
|
|
#include "missing.h"
|
2015-01-15 01:45:10 +00:00
|
|
|
#include "resource.h"
|
2013-01-19 04:04:54 +00:00
|
|
|
#include "msapi_utf8.h"
|
2016-02-20 22:52:32 +00:00
|
|
|
|
2014-05-22 00:52:25 +00:00
|
|
|
#include "drive.h"
|
2013-01-20 22:46:11 +00:00
|
|
|
#include "registry.h"
|
2014-12-29 20:34:41 +00:00
|
|
|
#include "bled/bled.h"
|
2013-01-20 22:46:11 +00:00
|
|
|
|
2014-05-22 00:52:25 +00:00
|
|
|
#define VHD_FOOTER_COOKIE { 'c', 'o', 'n', 'e', 'c', 't', 'i', 'x' }
|
|
|
|
|
2014-05-23 00:03:01 +00:00
|
|
|
#define VHD_FOOTER_FEATURES_NONE 0x00000000
|
|
|
|
#define VHD_FOOTER_FEATURES_TEMPORARY 0x00000001
|
|
|
|
#define VHD_FOOTER_FEATURES_RESERVED 0x00000002
|
|
|
|
|
2014-05-22 00:52:25 +00:00
|
|
|
#define VHD_FOOTER_FILE_FORMAT_V1_0 0x00010000
|
|
|
|
|
2014-05-23 00:03:01 +00:00
|
|
|
#define VHD_FOOTER_DATA_OFFSET_FIXED_DISK 0xFFFFFFFFFFFFFFFFULL
|
|
|
|
|
|
|
|
#define VHD_FOOTER_CREATOR_HOST_OS_WINDOWS { 'W', 'i', '2', 'k' }
|
|
|
|
#define VHD_FOOTER_CREATOR_HOST_OS_MAC { 'M', 'a', 'c', ' ' }
|
|
|
|
|
2014-05-22 00:52:25 +00:00
|
|
|
#define VHD_FOOTER_TYPE_FIXED_HARD_DISK 0x00000002
|
|
|
|
#define VHD_FOOTER_TYPE_DYNAMIC_HARD_DISK 0x00000003
|
|
|
|
#define VHD_FOOTER_TYPE_DIFFER_HARD_DISK 0x00000004
|
2013-01-19 04:04:54 +00:00
|
|
|
|
2020-07-19 21:35:30 +00:00
|
|
|
#define WIM_MAGIC 0x0000004D4957534DULL // "MSWIM\0\0\0"
|
|
|
|
#define WIM_HAS_API_EXTRACT 1
|
|
|
|
#define WIM_HAS_7Z_EXTRACT 2
|
|
|
|
#define WIM_HAS_API_APPLY 4
|
|
|
|
#define WIM_HAS_EXTRACT(r) (r & (WIM_HAS_API_EXTRACT|WIM_HAS_7Z_EXTRACT))
|
|
|
|
|
2014-05-23 00:03:01 +00:00
|
|
|
#define SECONDS_SINCE_JAN_1ST_2000 946684800
|
|
|
|
|
2014-05-22 00:52:25 +00:00
|
|
|
/*
|
|
|
|
* VHD Fixed HD footer (Big Endian)
|
|
|
|
* http://download.microsoft.com/download/f/f/e/ffef50a5-07dd-4cf8-aaa3-442c0673a029/Virtual%20Hard%20Disk%20Format%20Spec_10_18_06.doc
|
2014-05-23 00:03:01 +00:00
|
|
|
* NB: If a dymamic implementation is needed, check the GPL v3 compatible C++ implementation from:
|
|
|
|
* https://sourceforge.net/p/urbackup/backend/ci/master/tree/fsimageplugin/
|
2014-05-22 00:52:25 +00:00
|
|
|
*/
|
|
|
|
#pragma pack(push, 1)
|
|
|
|
typedef struct vhd_footer {
|
|
|
|
char cookie[8];
|
|
|
|
uint32_t features;
|
|
|
|
uint32_t file_format_version;
|
|
|
|
uint64_t data_offset;
|
|
|
|
uint32_t timestamp;
|
2014-05-23 00:03:01 +00:00
|
|
|
char creator_app[4];
|
2014-05-22 00:52:25 +00:00
|
|
|
uint32_t creator_version;
|
2014-05-23 00:03:01 +00:00
|
|
|
char creator_host_os[4];
|
2014-05-22 00:52:25 +00:00
|
|
|
uint64_t original_size;
|
|
|
|
uint64_t current_size;
|
2014-05-23 00:03:01 +00:00
|
|
|
union {
|
|
|
|
uint32_t geometry;
|
|
|
|
struct {
|
|
|
|
uint16_t cylinders;
|
|
|
|
uint8_t heads;
|
|
|
|
uint8_t sectors;
|
|
|
|
} chs;
|
|
|
|
} disk_geometry;
|
2014-05-22 00:52:25 +00:00
|
|
|
uint32_t disk_type;
|
|
|
|
uint32_t checksum;
|
|
|
|
uuid_t unique_id;
|
|
|
|
uint8_t saved_state;
|
|
|
|
uint8_t reserved[427];
|
|
|
|
} vhd_footer;
|
|
|
|
#pragma pack(pop)
|
2014-05-20 18:28:46 +00:00
|
|
|
|
2014-05-12 21:44:10 +00:00
|
|
|
// WIM API Prototypes
|
2019-01-08 18:30:07 +00:00
|
|
|
#define WIM_GENERIC_READ GENERIC_READ
|
|
|
|
#define WIM_OPEN_EXISTING OPEN_EXISTING
|
|
|
|
#define WIM_UNDOCUMENTED_BULLSHIT 0x20000000
|
2014-05-12 21:44:10 +00:00
|
|
|
PF_TYPE_DECL(WINAPI, HANDLE, WIMCreateFile, (PWSTR, DWORD, DWORD, DWORD, DWORD, PDWORD));
|
|
|
|
PF_TYPE_DECL(WINAPI, BOOL, WIMSetTemporaryPath, (HANDLE, PWSTR));
|
|
|
|
PF_TYPE_DECL(WINAPI, HANDLE, WIMLoadImage, (HANDLE, DWORD));
|
2020-07-19 21:35:30 +00:00
|
|
|
PF_TYPE_DECL(WINAPI, BOOL, WIMMountImage, (PCWSTR, PCWSTR, DWORD, PCWSTR));
|
|
|
|
PF_TYPE_DECL(WINAPI, BOOL, WIMUnmountImage, (PCWSTR, PCWSTR, DWORD, BOOL));
|
2015-01-15 01:45:10 +00:00
|
|
|
PF_TYPE_DECL(WINAPI, BOOL, WIMApplyImage, (HANDLE, PCWSTR, DWORD));
|
2014-05-12 21:44:10 +00:00
|
|
|
PF_TYPE_DECL(WINAPI, BOOL, WIMExtractImagePath, (HANDLE, PWSTR, PWSTR, DWORD));
|
2016-12-13 14:21:51 +00:00
|
|
|
PF_TYPE_DECL(WINAPI, BOOL, WIMGetImageInformation, (HANDLE, PVOID, PDWORD));
|
2014-05-12 21:44:10 +00:00
|
|
|
PF_TYPE_DECL(WINAPI, BOOL, WIMCloseHandle, (HANDLE));
|
2015-01-15 01:45:10 +00:00
|
|
|
PF_TYPE_DECL(WINAPI, DWORD, WIMRegisterMessageCallback, (HANDLE, FARPROC, PVOID));
|
|
|
|
PF_TYPE_DECL(WINAPI, DWORD, WIMUnregisterMessageCallback, (HANDLE, FARPROC));
|
2014-05-23 00:03:01 +00:00
|
|
|
PF_TYPE_DECL(RPC_ENTRY, RPC_STATUS, UuidCreate, (UUID __RPC_FAR*));
|
2013-01-20 22:46:11 +00:00
|
|
|
|
2019-08-25 13:09:28 +00:00
|
|
|
uint32_t wim_nb_files, wim_proc_files, wim_extra_files;
|
2021-10-08 18:22:33 +00:00
|
|
|
HANDLE wim_thread = NULL;
|
2020-07-10 10:55:29 +00:00
|
|
|
extern int default_thread_priority;
|
2020-11-12 17:38:20 +00:00
|
|
|
extern BOOL ignore_boot_marker;
|
2019-08-25 13:09:28 +00:00
|
|
|
|
2015-01-15 01:45:10 +00:00
|
|
|
static uint8_t wim_flags = 0;
|
2021-10-08 18:22:33 +00:00
|
|
|
static wchar_t wmount_path[MAX_PATH] = { 0 }, wmount_track[MAX_PATH] = { 0 };
|
2014-05-22 00:52:25 +00:00
|
|
|
static char sevenzip_path[MAX_PATH];
|
2014-05-23 00:03:01 +00:00
|
|
|
static const char conectix_str[] = VHD_FOOTER_COOKIE;
|
2015-01-22 22:31:34 +00:00
|
|
|
static BOOL count_files;
|
2021-10-08 18:22:33 +00:00
|
|
|
// Apply/Mount image functionality
|
|
|
|
static const char *_image, *_dst;
|
|
|
|
static int _index;
|
2014-05-22 00:52:25 +00:00
|
|
|
|
2013-02-02 16:57:46 +00:00
|
|
|
static BOOL Get7ZipPath(void)
|
|
|
|
{
|
|
|
|
if ( (GetRegistryKeyStr(REGKEY_HKCU, "7-Zip\\Path", sevenzip_path, sizeof(sevenzip_path)))
|
|
|
|
|| (GetRegistryKeyStr(REGKEY_HKLM, "7-Zip\\Path", sevenzip_path, sizeof(sevenzip_path))) ) {
|
2017-08-10 18:43:04 +00:00
|
|
|
static_strcat(sevenzip_path, "\\7z.exe");
|
2020-06-10 19:00:47 +00:00
|
|
|
return (_accessU(sevenzip_path, 0) != -1);
|
2013-02-02 16:57:46 +00:00
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-05-27 01:02:50 +00:00
|
|
|
BOOL AppendVHDFooter(const char* vhd_path)
|
2014-05-23 00:03:01 +00:00
|
|
|
{
|
|
|
|
const char creator_os[4] = VHD_FOOTER_CREATOR_HOST_OS_WINDOWS;
|
2015-06-07 21:51:54 +00:00
|
|
|
const char creator_app[4] = { 'r', 'u', 'f', 's' };
|
2014-05-23 00:03:01 +00:00
|
|
|
BOOL r = FALSE;
|
|
|
|
DWORD size;
|
|
|
|
LARGE_INTEGER li;
|
|
|
|
HANDLE handle = INVALID_HANDLE_VALUE;
|
2014-05-27 01:02:50 +00:00
|
|
|
vhd_footer* footer = NULL;
|
2014-05-23 00:03:01 +00:00
|
|
|
uint64_t totalSectors;
|
|
|
|
uint16_t cylinders = 0;
|
|
|
|
uint8_t heads, sectorsPerTrack;
|
|
|
|
uint32_t cylinderTimesHeads;
|
|
|
|
uint32_t checksum;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
PF_INIT(UuidCreate, Rpcrt4);
|
2014-11-11 19:17:39 +00:00
|
|
|
handle = CreateFileU(vhd_path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
|
|
|
|
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
2014-05-23 00:03:01 +00:00
|
|
|
li.QuadPart = 0;
|
|
|
|
if ((handle == INVALID_HANDLE_VALUE) || (!SetFilePointerEx(handle, li, &li, FILE_END))) {
|
2014-05-27 01:02:50 +00:00
|
|
|
uprintf("Could not open image '%s': %s", vhd_path, WindowsErrorString());
|
2014-05-23 00:03:01 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
footer = (vhd_footer*)calloc(1, sizeof(vhd_footer));
|
|
|
|
if (footer == NULL) {
|
|
|
|
uprintf("Could not allocate VHD footer");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(footer->cookie, conectix_str, sizeof(footer->cookie));
|
|
|
|
footer->features = bswap_uint32(VHD_FOOTER_FEATURES_RESERVED);
|
|
|
|
footer->file_format_version = bswap_uint32(VHD_FOOTER_FILE_FORMAT_V1_0);
|
|
|
|
footer->data_offset = bswap_uint64(VHD_FOOTER_DATA_OFFSET_FIXED_DISK);
|
2015-08-05 21:36:22 +00:00
|
|
|
footer->timestamp = bswap_uint32((uint32_t)(_time64(NULL) - SECONDS_SINCE_JAN_1ST_2000));
|
2014-05-23 00:03:01 +00:00
|
|
|
memcpy(footer->creator_app, creator_app, sizeof(creator_app));
|
|
|
|
footer->creator_version = bswap_uint32((rufus_version[0]<<16)|rufus_version[1]);
|
|
|
|
memcpy(footer->creator_host_os, creator_os, sizeof(creator_os));
|
|
|
|
footer->original_size = bswap_uint64(li.QuadPart);
|
|
|
|
footer->current_size = footer->original_size;
|
|
|
|
footer->disk_type = bswap_uint32(VHD_FOOTER_TYPE_FIXED_HARD_DISK);
|
|
|
|
if ((pfUuidCreate == NULL) || (pfUuidCreate(&footer->unique_id) != RPC_S_OK))
|
|
|
|
uprintf("Warning: could not set VHD UUID");
|
|
|
|
|
|
|
|
// Compute CHS, as per the VHD specs
|
|
|
|
totalSectors = li.QuadPart / 512;
|
|
|
|
if (totalSectors > 65535 * 16 * 255) {
|
|
|
|
totalSectors = 65535 * 16 * 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (totalSectors >= 65535 * 16 * 63) {
|
|
|
|
sectorsPerTrack = 255;
|
|
|
|
heads = 16;
|
|
|
|
cylinderTimesHeads = (uint32_t)(totalSectors / sectorsPerTrack);
|
|
|
|
} else {
|
2016-01-14 17:43:02 +00:00
|
|
|
sectorsPerTrack = 17;
|
2014-05-23 00:03:01 +00:00
|
|
|
cylinderTimesHeads = (uint32_t)(totalSectors / sectorsPerTrack);
|
|
|
|
|
|
|
|
heads = (cylinderTimesHeads + 1023) / 1024;
|
|
|
|
|
|
|
|
if (heads < 4) {
|
|
|
|
heads = 4;
|
|
|
|
}
|
|
|
|
if (cylinderTimesHeads >= ((uint32_t)heads * 1024) || heads > 16) {
|
|
|
|
sectorsPerTrack = 31;
|
|
|
|
heads = 16;
|
|
|
|
cylinderTimesHeads = (uint32_t)(totalSectors / sectorsPerTrack);
|
|
|
|
}
|
|
|
|
if (cylinderTimesHeads >= ((uint32_t)heads * 1024)) {
|
|
|
|
sectorsPerTrack = 63;
|
|
|
|
heads = 16;
|
|
|
|
cylinderTimesHeads = (uint32_t)(totalSectors / sectorsPerTrack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cylinders = cylinderTimesHeads / heads;
|
|
|
|
footer->disk_geometry.chs.cylinders = bswap_uint16(cylinders);
|
|
|
|
footer->disk_geometry.chs.heads = heads;
|
|
|
|
footer->disk_geometry.chs.sectors = sectorsPerTrack;
|
|
|
|
|
|
|
|
// Compute the VHD footer checksum
|
|
|
|
for (checksum=0, i=0; i<sizeof(vhd_footer); i++)
|
|
|
|
checksum += ((uint8_t*)footer)[i];
|
|
|
|
footer->checksum = bswap_uint32(~checksum);
|
|
|
|
|
2016-01-14 17:43:02 +00:00
|
|
|
if (!WriteFileWithRetry(handle, footer, sizeof(vhd_footer), &size, WRITE_RETRIES)) {
|
2014-05-23 00:03:01 +00:00
|
|
|
uprintf("Could not write VHD footer: %s", WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
r = TRUE;
|
2016-01-14 17:43:02 +00:00
|
|
|
|
2014-05-23 00:03:01 +00:00
|
|
|
out:
|
|
|
|
safe_free(footer);
|
|
|
|
safe_closehandle(handle);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-12-29 20:34:41 +00:00
|
|
|
typedef struct {
|
|
|
|
const char* ext;
|
|
|
|
bled_compression_type type;
|
|
|
|
} comp_assoc;
|
|
|
|
|
2015-02-11 23:22:18 +00:00
|
|
|
static comp_assoc file_assoc[] = {
|
2015-06-20 17:40:55 +00:00
|
|
|
{ ".zip", BLED_COMPRESSION_ZIP },
|
|
|
|
{ ".Z", BLED_COMPRESSION_LZW },
|
2014-12-29 20:34:41 +00:00
|
|
|
{ ".gz", BLED_COMPRESSION_GZIP },
|
|
|
|
{ ".lzma", BLED_COMPRESSION_LZMA },
|
|
|
|
{ ".bz2", BLED_COMPRESSION_BZIP2 },
|
2015-06-20 17:40:55 +00:00
|
|
|
{ ".xz", BLED_COMPRESSION_XZ },
|
2021-06-22 18:05:29 +00:00
|
|
|
{ ".vtsi", BLED_COMPRESSION_VTSI },
|
2014-12-29 20:34:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// For now we consider that an image that matches a known extension is bootable
|
2015-02-11 23:22:18 +00:00
|
|
|
#define MBR_SIZE 512 // Might need to review this once we see bootable 4k systems
|
2014-12-29 20:34:41 +00:00
|
|
|
BOOL IsCompressedBootableImage(const char* path)
|
|
|
|
{
|
2015-02-11 23:22:18 +00:00
|
|
|
char *p;
|
|
|
|
unsigned char *buf = NULL;
|
2014-12-29 20:34:41 +00:00
|
|
|
int i;
|
2015-02-11 23:22:18 +00:00
|
|
|
BOOL r = FALSE;
|
|
|
|
int64_t dc;
|
2014-12-29 20:34:41 +00:00
|
|
|
|
2015-09-02 22:20:00 +00:00
|
|
|
img_report.compression_type = BLED_COMPRESSION_NONE;
|
2014-12-29 20:34:41 +00:00
|
|
|
for (p = (char*)&path[strlen(path)-1]; (*p != '.') && (p != path); p--);
|
|
|
|
|
|
|
|
if (p == path)
|
|
|
|
return FALSE;
|
|
|
|
|
2015-02-11 23:22:18 +00:00
|
|
|
for (i = 0; i<ARRAYSIZE(file_assoc); i++) {
|
|
|
|
if (strcmp(p, file_assoc[i].ext) == 0) {
|
2015-09-02 22:20:00 +00:00
|
|
|
img_report.compression_type = file_assoc[i].type;
|
2015-02-11 23:22:18 +00:00
|
|
|
buf = malloc(MBR_SIZE);
|
|
|
|
if (buf == NULL)
|
|
|
|
return FALSE;
|
|
|
|
FormatStatus = 0;
|
2020-06-10 19:00:47 +00:00
|
|
|
bled_init(_uprintf, NULL, NULL, NULL, NULL, &FormatStatus);
|
2015-02-11 23:22:18 +00:00
|
|
|
dc = bled_uncompress_to_buffer(path, (char*)buf, MBR_SIZE, file_assoc[i].type);
|
|
|
|
bled_exit();
|
|
|
|
if (dc != MBR_SIZE) {
|
|
|
|
free(buf);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
r = (buf[0x1FE] == 0x55) && (buf[0x1FF] == 0xAA);
|
|
|
|
free(buf);
|
|
|
|
return r;
|
2014-12-29 20:34:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-11-12 17:38:20 +00:00
|
|
|
// 0: non-bootable, 1: bootable, 2: forced bootable
|
|
|
|
uint8_t IsBootableImage(const char* path)
|
2014-05-22 00:52:25 +00:00
|
|
|
{
|
|
|
|
HANDLE handle = INVALID_HANDLE_VALUE;
|
|
|
|
LARGE_INTEGER liImageSize;
|
|
|
|
vhd_footer* footer = NULL;
|
|
|
|
DWORD size;
|
2014-05-23 00:03:01 +00:00
|
|
|
size_t i;
|
|
|
|
uint32_t checksum, old_checksum;
|
2020-07-19 21:35:30 +00:00
|
|
|
uint64_t wim_magic = 0;
|
|
|
|
LARGE_INTEGER ptr = { 0 };
|
2020-11-12 17:38:20 +00:00
|
|
|
uint8_t is_bootable_img = 0;
|
2014-05-22 00:52:25 +00:00
|
|
|
|
2015-08-22 14:18:25 +00:00
|
|
|
uprintf("Disk image analysis:");
|
2014-11-11 19:17:39 +00:00
|
|
|
handle = CreateFileU(path, GENERIC_READ, FILE_SHARE_READ, NULL,
|
|
|
|
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
2014-05-22 00:52:25 +00:00
|
|
|
if (handle == INVALID_HANDLE_VALUE) {
|
2015-08-22 14:18:25 +00:00
|
|
|
uprintf(" Could not open image '%s'", path);
|
2014-05-22 00:52:25 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2014-12-29 20:34:41 +00:00
|
|
|
|
2020-11-12 17:38:20 +00:00
|
|
|
is_bootable_img = IsCompressedBootableImage(path) ? 1 : 0;
|
2015-09-02 22:20:00 +00:00
|
|
|
if (img_report.compression_type == BLED_COMPRESSION_NONE)
|
2020-11-12 17:38:20 +00:00
|
|
|
is_bootable_img = AnalyzeMBR(handle, " Image", FALSE) ? 1 : (ignore_boot_marker ? 2 : 0);
|
2014-05-22 00:52:25 +00:00
|
|
|
|
|
|
|
if (!GetFileSizeEx(handle, &liImageSize)) {
|
2015-08-22 14:18:25 +00:00
|
|
|
uprintf(" Could not get image size: %s", WindowsErrorString());
|
2014-05-22 00:52:25 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2017-05-01 15:22:34 +00:00
|
|
|
img_report.image_size = (uint64_t)liImageSize.QuadPart;
|
2020-07-19 21:35:30 +00:00
|
|
|
size = sizeof(wim_magic);
|
2020-07-29 18:32:32 +00:00
|
|
|
IGNORE_RETVAL(SetFilePointerEx(handle, ptr, NULL, FILE_BEGIN));
|
2020-07-19 21:35:30 +00:00
|
|
|
img_report.is_windows_img = ReadFile(handle, &wim_magic, size, &size, NULL) && (wim_magic == WIM_MAGIC);
|
|
|
|
if (img_report.is_windows_img)
|
|
|
|
goto out;
|
2014-05-22 00:52:25 +00:00
|
|
|
|
|
|
|
size = sizeof(vhd_footer);
|
2017-05-01 15:22:34 +00:00
|
|
|
if ((img_report.compression_type == BLED_COMPRESSION_NONE) && (img_report.image_size >= (512 + size))) {
|
2014-05-22 00:52:25 +00:00
|
|
|
footer = (vhd_footer*)malloc(size);
|
2017-05-01 15:22:34 +00:00
|
|
|
ptr.QuadPart = img_report.image_size - size;
|
2014-05-22 00:52:25 +00:00
|
|
|
if ( (footer == NULL) || (!SetFilePointerEx(handle, ptr, NULL, FILE_BEGIN)) ||
|
|
|
|
(!ReadFile(handle, footer, size, &size, NULL)) || (size != sizeof(vhd_footer)) ) {
|
2015-08-22 14:18:25 +00:00
|
|
|
uprintf(" Could not read VHD footer");
|
2014-05-22 00:52:25 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (memcmp(footer->cookie, conectix_str, sizeof(footer->cookie)) == 0) {
|
2017-05-01 15:22:34 +00:00
|
|
|
img_report.image_size -= sizeof(vhd_footer);
|
2014-05-22 00:52:25 +00:00
|
|
|
if ( (bswap_uint32(footer->file_format_version) != VHD_FOOTER_FILE_FORMAT_V1_0)
|
|
|
|
|| (bswap_uint32(footer->disk_type) != VHD_FOOTER_TYPE_FIXED_HARD_DISK)) {
|
2015-08-22 14:18:25 +00:00
|
|
|
uprintf(" Unsupported type of VHD image");
|
2020-11-12 17:38:20 +00:00
|
|
|
is_bootable_img = 0;
|
2014-05-22 00:52:25 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2014-05-23 00:03:01 +00:00
|
|
|
// Might as well validate the checksum while we're at it
|
|
|
|
old_checksum = bswap_uint32(footer->checksum);
|
|
|
|
footer->checksum = 0;
|
|
|
|
for (checksum=0, i=0; i<sizeof(vhd_footer); i++)
|
|
|
|
checksum += ((uint8_t*)footer)[i];
|
|
|
|
checksum = ~checksum;
|
|
|
|
if (checksum != old_checksum)
|
2015-08-22 14:18:25 +00:00
|
|
|
uprintf(" Warning: VHD footer seems corrupted (checksum: %04X, expected: %04X)", old_checksum, checksum);
|
2014-05-22 00:52:25 +00:00
|
|
|
// Need to remove the footer from our payload
|
2015-08-22 14:18:25 +00:00
|
|
|
uprintf(" Image is a Fixed Hard Disk VHD file");
|
2015-09-02 22:20:00 +00:00
|
|
|
img_report.is_vhd = TRUE;
|
2014-05-22 00:52:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
safe_free(footer);
|
|
|
|
safe_closehandle(handle);
|
2015-09-02 22:20:00 +00:00
|
|
|
return is_bootable_img;
|
2014-05-22 00:52:25 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 01:45:10 +00:00
|
|
|
// Find out if we have any way to extract/apply WIM files on this platform
|
|
|
|
// Returns a bitfield of the methods we can use (1 = Extract using wimgapi, 2 = Extract using 7-Zip, 4 = Apply using wimgapi)
|
2020-07-19 21:35:30 +00:00
|
|
|
uint8_t WimExtractCheck(BOOL bSilent)
|
2013-01-20 22:46:11 +00:00
|
|
|
{
|
2014-05-12 21:44:10 +00:00
|
|
|
PF_INIT(WIMCreateFile, Wimgapi);
|
|
|
|
PF_INIT(WIMSetTemporaryPath, Wimgapi);
|
|
|
|
PF_INIT(WIMLoadImage, Wimgapi);
|
2015-01-15 01:45:10 +00:00
|
|
|
PF_INIT(WIMApplyImage, Wimgapi);
|
2014-05-12 21:44:10 +00:00
|
|
|
PF_INIT(WIMExtractImagePath, Wimgapi);
|
2016-12-13 14:21:51 +00:00
|
|
|
PF_INIT(WIMGetImageInformation, Wimgapi);
|
2015-01-15 01:45:10 +00:00
|
|
|
PF_INIT(WIMRegisterMessageCallback, Wimgapi);
|
|
|
|
PF_INIT(WIMUnregisterMessageCallback, Wimgapi);
|
2014-05-12 21:44:10 +00:00
|
|
|
PF_INIT(WIMCloseHandle, Wimgapi);
|
2013-01-20 22:46:11 +00:00
|
|
|
|
2015-01-15 01:45:10 +00:00
|
|
|
if (pfWIMCreateFile && pfWIMSetTemporaryPath && pfWIMLoadImage && pfWIMExtractImagePath && pfWIMCloseHandle)
|
|
|
|
wim_flags |= WIM_HAS_API_EXTRACT;
|
|
|
|
if (Get7ZipPath())
|
|
|
|
wim_flags |= WIM_HAS_7Z_EXTRACT;
|
|
|
|
if ((wim_flags & WIM_HAS_API_EXTRACT) && pfWIMApplyImage && pfWIMRegisterMessageCallback && pfWIMUnregisterMessageCallback)
|
|
|
|
wim_flags |= WIM_HAS_API_APPLY;
|
|
|
|
|
2020-07-19 21:35:30 +00:00
|
|
|
suprintf("WIM extraction method(s) supported: %s%s%s", (wim_flags & WIM_HAS_7Z_EXTRACT)?"7-Zip":
|
2015-01-15 01:45:10 +00:00
|
|
|
((wim_flags & WIM_HAS_API_EXTRACT)?"":"NONE"),
|
2015-01-26 23:20:39 +00:00
|
|
|
(WIM_HAS_EXTRACT(wim_flags) == (WIM_HAS_API_EXTRACT|WIM_HAS_7Z_EXTRACT))?", ":
|
|
|
|
"", (wim_flags & WIM_HAS_API_EXTRACT)?"wimgapi.dll":"");
|
2020-07-19 21:35:30 +00:00
|
|
|
suprintf("WIM apply method supported: %s", (wim_flags & WIM_HAS_API_APPLY)?"wimgapi.dll":"NONE");
|
2015-01-15 01:45:10 +00:00
|
|
|
return wim_flags;
|
2013-01-20 22:46:11 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 18:22:33 +00:00
|
|
|
// Looks like Microsoft's idea of "mount" for WIM images involves the creation
|
|
|
|
// of a as many virtual junctions as there exist directories on the image...
|
|
|
|
// So, yeah, this is both very slow and wasteful of space.
|
|
|
|
static DWORD WINAPI WimMountImageThread(LPVOID param)
|
2020-07-19 21:35:30 +00:00
|
|
|
{
|
|
|
|
BOOL r = FALSE;
|
|
|
|
wconvert(temp_dir);
|
2021-10-08 18:22:33 +00:00
|
|
|
wchar_t* wimage = utf8_to_wchar(_image);
|
2020-07-19 21:35:30 +00:00
|
|
|
PF_INIT_OR_OUT(WIMMountImage, Wimgapi);
|
|
|
|
|
|
|
|
if (wmount_path[0] != 0) {
|
|
|
|
uprintf("WimMountImage: An image is already mounted");
|
|
|
|
goto out;
|
|
|
|
}
|
2021-10-08 18:22:33 +00:00
|
|
|
if (GetTempFileNameW(wtemp_dir, L"Ruf", 0, wmount_path) == 0) {
|
|
|
|
uprintf("WimMountImage: Can not generate mount directory: %s", WindowsErrorString());
|
2020-07-19 21:35:30 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
DeleteFileW(wmount_path);
|
|
|
|
if (!CreateDirectoryW(wmount_path, 0)) {
|
2021-10-08 18:22:33 +00:00
|
|
|
uprintf("WimMountImage: Can not create mount directory '%S': %s", wmount_path, WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (GetTempFileNameW(wtemp_dir, L"Ruf", 0, wmount_track) == 0) {
|
|
|
|
uprintf("WimMountImage: Can not generate tracking directory: %s", WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
DeleteFileW(wmount_track);
|
|
|
|
if (!CreateDirectoryW(wmount_track, 0)) {
|
|
|
|
uprintf("WimMountImage: Can not create tracking directory '%S': %s", wmount_track, WindowsErrorString());
|
2020-07-19 21:35:30 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2021-10-08 18:22:33 +00:00
|
|
|
r = pfWIMMountImage(wmount_path, wimage, _index, wmount_track);
|
|
|
|
if (!r) {
|
|
|
|
uprintf("Could not mount '%S [%d]' on '%S': %s", wimage, _index, wmount_path, WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
uprintf("mounted '%S [%d]' on '%S'", wimage, _index, wmount_path);
|
2020-07-19 21:35:30 +00:00
|
|
|
|
|
|
|
out:
|
|
|
|
wfree(temp_dir);
|
2021-10-08 18:22:33 +00:00
|
|
|
safe_free(wimage);
|
|
|
|
ExitThread((DWORD)r);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the temporary mount path on success, NULL on error.
|
|
|
|
// Returned path must be freed by the caller.
|
|
|
|
char* WimMountImage(const char* image, int index)
|
|
|
|
{
|
|
|
|
DWORD dw = 0;
|
|
|
|
_image = image;
|
|
|
|
_index = index;
|
|
|
|
|
|
|
|
wim_thread = CreateThread(NULL, 0, WimMountImageThread, NULL, 0, NULL);
|
|
|
|
if (wim_thread == NULL) {
|
|
|
|
uprintf("Unable to start mount-image thread");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
SetThreadPriority(wim_thread, default_thread_priority);
|
|
|
|
WaitForSingleObject(wim_thread, INFINITE);
|
|
|
|
if (!GetExitCodeThread(wim_thread, &dw))
|
|
|
|
dw = 0;
|
|
|
|
wim_thread = NULL;
|
|
|
|
return (dw) ? wchar_to_utf8(wmount_path) : NULL;
|
2020-07-19 21:35:30 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 18:22:33 +00:00
|
|
|
static DWORD WINAPI WimUnmountImageThread(LPVOID param)
|
2020-07-19 21:35:30 +00:00
|
|
|
{
|
|
|
|
BOOL r = FALSE;
|
2021-10-08 18:22:33 +00:00
|
|
|
wchar_t* wimage = utf8_to_wchar(_image);
|
2020-07-19 21:35:30 +00:00
|
|
|
PF_INIT_OR_OUT(WIMUnmountImage, Wimgapi);
|
2021-10-08 18:22:33 +00:00
|
|
|
|
2020-07-19 21:35:30 +00:00
|
|
|
if (wmount_path[0] == 0) {
|
|
|
|
uprintf("WimUnmountImage: No image is mounted");
|
|
|
|
goto out;
|
|
|
|
}
|
2021-10-08 18:22:33 +00:00
|
|
|
r = pfWIMUnmountImage(wmount_path, wimage, _index, TRUE);
|
|
|
|
if (!r) {
|
|
|
|
uprintf("Could not unmount '%S': %s", wmount_path, WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
uprintf("Unmounted '%S [%d]'", wmount_path, _index);
|
|
|
|
RemoveDirectoryW(wmount_path);
|
2020-07-19 21:35:30 +00:00
|
|
|
wmount_path[0] = 0;
|
2021-10-08 18:22:33 +00:00
|
|
|
RemoveDirectoryW(wmount_track);
|
|
|
|
wmount_track[0] = 0;
|
2020-07-19 21:35:30 +00:00
|
|
|
out:
|
2021-10-08 18:22:33 +00:00
|
|
|
safe_free(wimage);
|
|
|
|
ExitThread((DWORD)r);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WimUnmountImage(const char* image, int index)
|
|
|
|
{
|
|
|
|
DWORD dw = 0;
|
|
|
|
_image = image;
|
|
|
|
_index = index;
|
|
|
|
|
|
|
|
wim_thread = CreateThread(NULL, 0, WimUnmountImageThread, NULL, 0, NULL);
|
|
|
|
if (wim_thread == NULL) {
|
|
|
|
uprintf("Unable to start unmount-image thread");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
SetThreadPriority(wim_thread, default_thread_priority);
|
|
|
|
WaitForSingleObject(wim_thread, INFINITE);
|
|
|
|
if (!GetExitCodeThread(wim_thread, &dw))
|
|
|
|
dw = 0;
|
|
|
|
wim_thread = NULL;
|
|
|
|
return dw;
|
2020-07-19 21:35:30 +00:00
|
|
|
}
|
2013-01-20 22:46:11 +00:00
|
|
|
|
|
|
|
// Extract a file from a WIM image using wimgapi.dll (Windows 7 or later)
|
2014-01-31 02:51:28 +00:00
|
|
|
// NB: if you want progress from a WIM callback, you must run the WIM API call in its own thread
|
|
|
|
// (which we don't do here) as it won't work otherwise. Thanks go to Erwan for figuring this out!
|
2020-07-19 21:35:30 +00:00
|
|
|
BOOL WimExtractFile_API(const char* image, int index, const char* src, const char* dst, BOOL bSilent)
|
2013-01-19 04:04:54 +00:00
|
|
|
{
|
2016-12-13 14:21:51 +00:00
|
|
|
static char* index_name = "[1].xml";
|
2013-01-19 04:04:54 +00:00
|
|
|
BOOL r = FALSE;
|
|
|
|
DWORD dw = 0;
|
|
|
|
HANDLE hWim = NULL;
|
|
|
|
HANDLE hImage = NULL;
|
2016-12-13 14:21:51 +00:00
|
|
|
HANDLE hFile = NULL;
|
2013-01-19 04:04:54 +00:00
|
|
|
wchar_t wtemp[MAX_PATH] = {0};
|
|
|
|
wchar_t* wimage = utf8_to_wchar(image);
|
|
|
|
wchar_t* wsrc = utf8_to_wchar(src);
|
|
|
|
wchar_t* wdst = utf8_to_wchar(dst);
|
2016-12-13 14:21:51 +00:00
|
|
|
char* wim_info;
|
2013-01-19 04:04:54 +00:00
|
|
|
|
2014-05-12 21:44:10 +00:00
|
|
|
PF_INIT_OR_OUT(WIMCreateFile, Wimgapi);
|
|
|
|
PF_INIT_OR_OUT(WIMSetTemporaryPath, Wimgapi);
|
|
|
|
PF_INIT_OR_OUT(WIMLoadImage, Wimgapi);
|
|
|
|
PF_INIT_OR_OUT(WIMExtractImagePath, Wimgapi);
|
|
|
|
PF_INIT_OR_OUT(WIMCloseHandle, Wimgapi);
|
2013-01-19 04:04:54 +00:00
|
|
|
|
2020-07-19 21:35:30 +00:00
|
|
|
suprintf("Opening: %s:[%d] (API)", image, index);
|
2013-01-19 04:04:54 +00:00
|
|
|
if (GetTempPathW(ARRAYSIZE(wtemp), wtemp) == 0) {
|
2015-01-15 01:45:10 +00:00
|
|
|
uprintf(" Could not fetch temp path: %s", WindowsErrorString());
|
2013-01-19 04:04:54 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2019-01-08 18:30:07 +00:00
|
|
|
// Thanks to dism++ for figuring out that you can use UNDOCUMENTED FLAG 0x20000000
|
|
|
|
// to open newer install.wim/install.esd images, without running into obnoxious error:
|
|
|
|
// [0x0000000B] An attempt was made to load a program with an incorrect format.
|
|
|
|
// No thanks to Microsoft for NOT DOCUMENTING THEIR UTTER BULLSHIT with the WIM API!
|
|
|
|
hWim = pfWIMCreateFile(wimage, WIM_GENERIC_READ, WIM_OPEN_EXISTING,
|
|
|
|
(img_report.wininst_version >= SPECIAL_WIM_VERSION) ? WIM_UNDOCUMENTED_BULLSHIT : 0, 0, NULL);
|
2013-01-19 04:04:54 +00:00
|
|
|
if (hWim == NULL) {
|
2015-01-15 01:45:10 +00:00
|
|
|
uprintf(" Could not access image: %s", WindowsErrorString());
|
2013-01-19 04:04:54 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pfWIMSetTemporaryPath(hWim, wtemp)) {
|
2015-01-15 01:45:10 +00:00
|
|
|
uprintf(" Could not set temp path: %s", WindowsErrorString());
|
2013-01-19 04:04:54 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2020-07-19 21:35:30 +00:00
|
|
|
suprintf("Extracting: %s (From %s)", dst, src);
|
2016-12-13 14:21:51 +00:00
|
|
|
if (safe_strcmp(src, index_name) == 0) {
|
|
|
|
if (!pfWIMGetImageInformation(hWim, &wim_info, &dw)) {
|
|
|
|
uprintf(" Could not access WIM info: %s", WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
2017-02-16 14:13:30 +00:00
|
|
|
hFile = CreateFileW(wdst, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ,
|
2016-12-13 14:21:51 +00:00
|
|
|
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
if ((hFile == INVALID_HANDLE_VALUE) || (!WriteFile(hFile, wim_info, dw, &dw, NULL))) {
|
2020-07-19 21:35:30 +00:00
|
|
|
suprintf(" Could not extract file: %s", WindowsErrorString());
|
2016-12-13 14:21:51 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
hImage = pfWIMLoadImage(hWim, (DWORD)index);
|
|
|
|
if (hImage == NULL) {
|
|
|
|
uprintf(" Could not set index: %s", WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (!pfWIMExtractImagePath(hImage, wsrc, wdst, 0)) {
|
2020-07-19 21:35:30 +00:00
|
|
|
suprintf(" Could not extract file: %s", WindowsErrorString());
|
2016-12-13 14:21:51 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2013-01-19 04:04:54 +00:00
|
|
|
}
|
|
|
|
r = TRUE;
|
|
|
|
|
|
|
|
out:
|
|
|
|
if ((hImage != NULL) || (hWim != NULL)) {
|
2020-07-19 21:35:30 +00:00
|
|
|
suprintf("Closing: %s", image);
|
2015-01-15 01:45:10 +00:00
|
|
|
if (hImage != NULL) pfWIMCloseHandle(hImage);
|
|
|
|
if (hWim != NULL) pfWIMCloseHandle(hWim);
|
2013-01-19 04:04:54 +00:00
|
|
|
}
|
2016-12-13 14:21:51 +00:00
|
|
|
safe_closehandle(hFile);
|
2013-01-19 04:04:54 +00:00
|
|
|
safe_free(wimage);
|
|
|
|
safe_free(wsrc);
|
|
|
|
safe_free(wdst);
|
|
|
|
return r;
|
|
|
|
}
|
2013-01-20 22:46:11 +00:00
|
|
|
|
|
|
|
// Extract a file from a WIM image using 7-Zip
|
2020-07-19 21:35:30 +00:00
|
|
|
BOOL WimExtractFile_7z(const char* image, int index, const char* src, const char* dst, BOOL bSilent)
|
2013-01-20 22:46:11 +00:00
|
|
|
{
|
2016-01-26 18:00:20 +00:00
|
|
|
int n;
|
2013-01-20 22:46:11 +00:00
|
|
|
size_t i;
|
|
|
|
char cmdline[MAX_PATH];
|
|
|
|
char tmpdst[MAX_PATH];
|
2016-01-26 18:00:20 +00:00
|
|
|
char index_prefix[] = "#\\";
|
2013-01-20 22:46:11 +00:00
|
|
|
|
2020-07-19 21:35:30 +00:00
|
|
|
suprintf("Opening: %s:[%d] (7-Zip)", image, index);
|
2015-08-10 22:19:57 +00:00
|
|
|
|
|
|
|
if ((image == NULL) || (src == NULL) || (dst == NULL))
|
|
|
|
return FALSE;
|
|
|
|
|
2016-01-26 18:00:20 +00:00
|
|
|
// If you shove more than 9 images in a WIM, don't come complaining
|
|
|
|
// that this breaks!
|
|
|
|
index_prefix[0] = '0' + index;
|
2013-01-20 22:46:11 +00:00
|
|
|
|
2020-07-19 21:35:30 +00:00
|
|
|
suprintf("Extracting: %s (From %s)", dst, src);
|
2016-01-26 18:00:20 +00:00
|
|
|
|
|
|
|
// 7z has a quirk where the image index MUST be specified if a
|
|
|
|
// WIM has multiple indexes, but it MUST be removed if there is
|
|
|
|
// only one image. Because of this (and because 7z will not
|
|
|
|
// return an error code if it can't extract the file), we need
|
|
|
|
// to issue 2 passes. See github issue #680.
|
|
|
|
for (n = 0; n < 2; n++) {
|
2017-08-10 18:43:04 +00:00
|
|
|
static_strcpy(tmpdst, dst);
|
2016-12-13 14:21:51 +00:00
|
|
|
for (i = strlen(tmpdst) - 1; (i > 0) && (tmpdst[i] != '\\') && (tmpdst[i] != '/'); i--);
|
2016-01-26 18:00:20 +00:00
|
|
|
tmpdst[i] = 0;
|
|
|
|
|
2017-08-10 18:43:04 +00:00
|
|
|
static_sprintf(cmdline, "\"%s\" -y e \"%s\" %s%s", sevenzip_path,
|
2016-01-26 18:00:20 +00:00
|
|
|
image, (n == 0) ? index_prefix : "", src);
|
|
|
|
if (RunCommand(cmdline, tmpdst, FALSE) != 0) {
|
|
|
|
uprintf(" Could not launch 7z.exe: %s", WindowsErrorString());
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2016-12-13 14:21:51 +00:00
|
|
|
for (i = safe_strlen(src); (i > 0) && (src[i] != '\\') && (src[i] != '/'); i--);
|
|
|
|
if (i == 0)
|
2017-08-10 18:43:04 +00:00
|
|
|
static_strcat(tmpdst, "\\");
|
|
|
|
static_strcat(tmpdst, &src[i]);
|
2016-01-26 18:00:20 +00:00
|
|
|
if (_access(tmpdst, 0) == 0)
|
|
|
|
// File was extracted => move on
|
|
|
|
break;
|
2013-01-20 22:46:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 18:00:20 +00:00
|
|
|
if (n >= 2) {
|
2020-07-19 21:35:30 +00:00
|
|
|
suprintf(" 7z.exe did not extract %s", tmpdst);
|
2013-01-20 22:46:11 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2016-01-26 18:00:20 +00:00
|
|
|
|
2014-05-09 21:05:25 +00:00
|
|
|
// coverity[toctou]
|
2020-07-19 21:35:30 +00:00
|
|
|
if (!MoveFileExU(tmpdst, dst, MOVEFILE_REPLACE_EXISTING)) {
|
|
|
|
uprintf(" Could not rename %s to %s: %s", tmpdst, dst, WindowsErrorString());
|
2013-01-20 22:46:11 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract a file from a WIM image
|
2020-07-19 21:35:30 +00:00
|
|
|
BOOL WimExtractFile(const char* image, int index, const char* src, const char* dst, BOOL bSilent)
|
2013-01-20 22:46:11 +00:00
|
|
|
{
|
2020-07-19 21:35:30 +00:00
|
|
|
if ((wim_flags == 0) && (!WIM_HAS_EXTRACT(WimExtractCheck(TRUE))))
|
2013-01-20 22:46:11 +00:00
|
|
|
return FALSE;
|
2013-01-20 23:34:13 +00:00
|
|
|
if ((image == NULL) || (src == NULL) || (dst == NULL))
|
|
|
|
return FALSE;
|
2013-01-20 22:46:11 +00:00
|
|
|
|
|
|
|
// Prefer 7-Zip as, unsurprisingly, it's faster than the Microsoft way,
|
|
|
|
// but allow fallback if 7-Zip doesn't succeed
|
2020-07-19 21:35:30 +00:00
|
|
|
return ( ((wim_flags & WIM_HAS_7Z_EXTRACT) && WimExtractFile_7z(image, index, src, dst, bSilent))
|
|
|
|
|| ((wim_flags & WIM_HAS_API_EXTRACT) && WimExtractFile_API(image, index, src, dst, bSilent)) );
|
2015-01-15 01:45:10 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 18:22:33 +00:00
|
|
|
// From https://docs.microsoft.com/en-us/previous-versions/msdn10/dd834960(v=msdn.10)
|
|
|
|
// as well as https://msfn.org/board/topic/150700-wimgapi-wimmountimage-progressbar/
|
2015-01-15 01:45:10 +00:00
|
|
|
enum WIMMessage {
|
|
|
|
WIM_MSG = WM_APP + 0x1476,
|
|
|
|
WIM_MSG_TEXT,
|
|
|
|
WIM_MSG_PROGRESS, // Indicates an update in the progress of an image application.
|
|
|
|
WIM_MSG_PROCESS, // Enables the caller to prevent a file or a directory from being captured or applied.
|
|
|
|
WIM_MSG_SCANNING, // Indicates that volume information is being gathered during an image capture.
|
|
|
|
WIM_MSG_SETRANGE, // Indicates the number of files that will be captured or applied.
|
|
|
|
WIM_MSG_SETPOS, // Indicates the number of files that have been captured or applied.
|
|
|
|
WIM_MSG_STEPIT, // Indicates that a file has been either captured or applied.
|
|
|
|
WIM_MSG_COMPRESS, // Enables the caller to prevent a file resource from being compressed during a capture.
|
|
|
|
WIM_MSG_ERROR, // Alerts the caller that an error has occurred while capturing or applying an image.
|
|
|
|
WIM_MSG_ALIGNMENT, // Enables the caller to align a file resource on a particular alignment boundary.
|
|
|
|
WIM_MSG_RETRY, // Sent when the file is being reapplied because of a network timeout.
|
|
|
|
WIM_MSG_SPLIT, // Enables the caller to align a file resource on a particular alignment boundary.
|
2015-01-22 22:31:34 +00:00
|
|
|
WIM_MSG_FILEINFO, // Used in conjunction with WimApplyImages()'s WIM_FLAG_FILEINFO flag to provide detailed file info.
|
2015-01-15 01:45:10 +00:00
|
|
|
WIM_MSG_INFO, // Sent when an info message is available.
|
|
|
|
WIM_MSG_WARNING, // Sent when a warning message is available.
|
|
|
|
WIM_MSG_CHK_PROCESS,
|
|
|
|
WIM_MSG_SUCCESS = 0x00000000,
|
2016-06-04 16:10:33 +00:00
|
|
|
WIM_MSG_ABORT_IMAGE = -1
|
2015-01-15 01:45:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define INVALID_CALLBACK_VALUE 0xFFFFFFFF
|
|
|
|
|
2015-01-22 22:31:34 +00:00
|
|
|
#define WIM_FLAG_RESERVED 0x00000001
|
|
|
|
#define WIM_FLAG_VERIFY 0x00000002
|
|
|
|
#define WIM_FLAG_INDEX 0x00000004
|
|
|
|
#define WIM_FLAG_NO_APPLY 0x00000008
|
|
|
|
#define WIM_FLAG_NO_DIRACL 0x00000010
|
|
|
|
#define WIM_FLAG_NO_FILEACL 0x00000020
|
|
|
|
#define WIM_FLAG_SHARE_WRITE 0x00000040
|
|
|
|
#define WIM_FLAG_FILEINFO 0x00000080
|
|
|
|
#define WIM_FLAG_NO_RP_FIX 0x00000100
|
|
|
|
|
2015-01-15 01:45:10 +00:00
|
|
|
// Progress callback
|
|
|
|
DWORD WINAPI WimProgressCallback(DWORD dwMsgId, WPARAM wParam, LPARAM lParam, PVOID pvIgnored)
|
|
|
|
{
|
|
|
|
PBOOL pbCancel = NULL;
|
2015-01-22 22:31:34 +00:00
|
|
|
PWIN32_FIND_DATA pFileData;
|
2015-01-15 01:45:10 +00:00
|
|
|
const char* level = NULL;
|
2015-01-22 22:31:34 +00:00
|
|
|
uint64_t size;
|
2015-01-15 01:45:10 +00:00
|
|
|
|
|
|
|
switch (dwMsgId) {
|
|
|
|
case WIM_MSG_PROGRESS:
|
2015-01-22 22:31:34 +00:00
|
|
|
// The default WIM progress is useless (freezes at 95%, which is usually when only half
|
|
|
|
// the files have been processed), so we don't use it
|
|
|
|
#if 0
|
2015-01-20 21:50:24 +00:00
|
|
|
PrintInfo(0, MSG_267, (DWORD)wParam);
|
2019-08-20 17:06:07 +00:00
|
|
|
UpdateProgress(OP_FILE_COPY, 0.98f*(DWORD)wParam);
|
2015-01-22 22:31:34 +00:00
|
|
|
#endif
|
2015-01-15 01:45:10 +00:00
|
|
|
break;
|
|
|
|
case WIM_MSG_PROCESS:
|
2015-01-22 22:31:34 +00:00
|
|
|
// The amount of files processed is overwhelming (16k+ for a typical image),
|
|
|
|
// and trying to display it *WILL* slow us down, so we don't.
|
2015-01-16 01:51:24 +00:00
|
|
|
#if 0
|
2019-01-03 12:29:28 +00:00
|
|
|
uprintf("%S", (PWSTR)wParam);
|
2015-01-15 01:45:10 +00:00
|
|
|
PrintStatus(0, MSG_000, str); // MSG_000 is "%s"
|
|
|
|
#endif
|
2015-01-22 22:31:34 +00:00
|
|
|
if (count_files) {
|
|
|
|
wim_nb_files++;
|
|
|
|
} else {
|
2019-08-25 13:09:28 +00:00
|
|
|
// At the end of an actual apply, the WIM API re-lists a bunch of directories it already processed,
|
|
|
|
// so, even as we try to compensate, we might end up with more entries than counted - ignore those.
|
|
|
|
if (wim_proc_files < wim_nb_files)
|
|
|
|
wim_proc_files++;
|
|
|
|
else
|
|
|
|
wim_extra_files++;
|
|
|
|
UpdateProgressWithInfo(OP_FILE_COPY, MSG_267, wim_proc_files, wim_nb_files);
|
2015-01-22 22:31:34 +00:00
|
|
|
}
|
|
|
|
// Halt on error
|
2015-01-15 01:45:10 +00:00
|
|
|
if (IS_ERROR(FormatStatus)) {
|
|
|
|
pbCancel = (PBOOL)lParam;
|
|
|
|
*pbCancel = TRUE;
|
2015-01-22 22:31:34 +00:00
|
|
|
break;
|
2015-01-15 01:45:10 +00:00
|
|
|
}
|
|
|
|
break;
|
2015-01-22 22:31:34 +00:00
|
|
|
case WIM_MSG_FILEINFO:
|
|
|
|
pFileData = (PWIN32_FIND_DATA)lParam;
|
2017-06-20 13:10:22 +00:00
|
|
|
if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
2019-01-03 12:29:28 +00:00
|
|
|
uprintf("Creating: %S", (PWSTR)wParam);
|
2017-06-20 13:10:22 +00:00
|
|
|
} else {
|
|
|
|
size = (((uint64_t)pFileData->nFileSizeHigh) << 32) + pFileData->nFileSizeLow;
|
2019-01-03 12:29:28 +00:00
|
|
|
uprintf("Extracting: %S (%s)", (PWSTR)wParam, SizeToHumanReadable(size, FALSE, FALSE));
|
2017-06-20 13:10:22 +00:00
|
|
|
}
|
2015-01-22 22:31:34 +00:00
|
|
|
break;
|
2015-01-15 01:45:10 +00:00
|
|
|
case WIM_MSG_RETRY:
|
|
|
|
level = "retry";
|
|
|
|
// fall through
|
|
|
|
case WIM_MSG_INFO:
|
|
|
|
if (level == NULL) level = "info";
|
|
|
|
// fall through
|
|
|
|
case WIM_MSG_WARNING:
|
|
|
|
if (level == NULL) level = "warning";
|
|
|
|
// fall through
|
|
|
|
case WIM_MSG_ERROR:
|
|
|
|
if (level == NULL) level = "error";
|
|
|
|
SetLastError((DWORD)lParam);
|
2019-01-03 12:29:28 +00:00
|
|
|
uprintf("Apply %s: %S [err = %d]\n", level, (PWSTR)wParam, WindowsErrorString());
|
2015-01-15 01:45:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return IS_ERROR(FormatStatus)?WIM_MSG_ABORT_IMAGE:WIM_MSG_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply a WIM image using wimgapi.dll (Windows 7 or later)
|
2021-10-08 18:22:33 +00:00
|
|
|
// https://docs.microsoft.com/en-us/previous-versions/msdn10/dd851944(v=msdn.10)
|
2015-01-15 01:45:10 +00:00
|
|
|
// To get progress, we must run this call within its own thread
|
|
|
|
static DWORD WINAPI WimApplyImageThread(LPVOID param)
|
|
|
|
{
|
|
|
|
BOOL r = FALSE;
|
|
|
|
HANDLE hWim = NULL;
|
|
|
|
HANDLE hImage = NULL;
|
|
|
|
wchar_t wtemp[MAX_PATH] = {0};
|
|
|
|
wchar_t* wimage = utf8_to_wchar(_image);
|
|
|
|
wchar_t* wdst = utf8_to_wchar(_dst);
|
|
|
|
|
|
|
|
PF_INIT_OR_OUT(WIMRegisterMessageCallback, Wimgapi);
|
|
|
|
PF_INIT_OR_OUT(WIMCreateFile, Wimgapi);
|
|
|
|
PF_INIT_OR_OUT(WIMSetTemporaryPath, Wimgapi);
|
|
|
|
PF_INIT_OR_OUT(WIMLoadImage, Wimgapi);
|
|
|
|
PF_INIT_OR_OUT(WIMApplyImage, Wimgapi);
|
|
|
|
PF_INIT_OR_OUT(WIMCloseHandle, Wimgapi);
|
|
|
|
PF_INIT_OR_OUT(WIMUnregisterMessageCallback, Wimgapi);
|
|
|
|
|
|
|
|
uprintf("Opening: %s:[%d]", _image, _index);
|
|
|
|
|
|
|
|
if (pfWIMRegisterMessageCallback(NULL, (FARPROC)WimProgressCallback, NULL) == INVALID_CALLBACK_VALUE) {
|
|
|
|
uprintf(" Could not set progress callback: %s", WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetTempPathW(ARRAYSIZE(wtemp), wtemp) == 0) {
|
|
|
|
uprintf(" Could not fetch temp path: %s", WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2019-01-08 18:30:07 +00:00
|
|
|
hWim = pfWIMCreateFile(wimage, WIM_GENERIC_READ, WIM_OPEN_EXISTING,
|
|
|
|
(img_report.wininst_version >= SPECIAL_WIM_VERSION) ? WIM_UNDOCUMENTED_BULLSHIT : 0, 0, NULL);
|
2015-01-15 01:45:10 +00:00
|
|
|
if (hWim == NULL) {
|
|
|
|
uprintf(" Could not access image: %s", WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pfWIMSetTemporaryPath(hWim, wtemp)) {
|
|
|
|
uprintf(" Could not set temp path: %s", WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
hImage = pfWIMLoadImage(hWim, (DWORD)_index);
|
|
|
|
if (hImage == NULL) {
|
|
|
|
uprintf(" Could not set index: %s", WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-01-16 01:51:24 +00:00
|
|
|
uprintf("Applying Windows image...");
|
2020-11-14 19:38:56 +00:00
|
|
|
UpdateProgressWithInfoInit(NULL, TRUE);
|
2015-01-22 22:31:34 +00:00
|
|
|
// Run a first pass using WIM_FLAG_NO_APPLY to count the files
|
|
|
|
wim_nb_files = 0;
|
|
|
|
wim_proc_files = 0;
|
2019-08-25 13:09:28 +00:00
|
|
|
wim_extra_files = 0;
|
2015-01-22 22:31:34 +00:00
|
|
|
count_files = TRUE;
|
|
|
|
if (!pfWIMApplyImage(hImage, wdst, WIM_FLAG_NO_APPLY)) {
|
|
|
|
uprintf(" Could not count the files to apply: %s", WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
2019-08-25 13:09:28 +00:00
|
|
|
// The latest Windows 10 ISOs have a ~17.5% discrepancy between the number of
|
|
|
|
// files and directories actually applied vs. the ones counted when not applying.
|
|
|
|
// Therefore, we add a 'safe' 20% to our counted files to compensate for yet
|
|
|
|
// another dismal Microsoft progress reporting API...
|
|
|
|
wim_nb_files += wim_nb_files / 5;
|
2015-01-22 22:31:34 +00:00
|
|
|
count_files = FALSE;
|
|
|
|
// Actual apply
|
2017-06-20 13:10:22 +00:00
|
|
|
if (!pfWIMApplyImage(hImage, wdst, WIM_FLAG_FILEINFO)) {
|
2015-01-15 01:45:10 +00:00
|
|
|
uprintf(" Could not apply image: %s", WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
2019-08-25 13:09:28 +00:00
|
|
|
// Ensure that we'll pick if need to readjust our 20% above from user reports
|
|
|
|
if (wim_extra_files > 0)
|
|
|
|
uprintf("Notice: An extra %d files and directories were applied, from the %d expected",
|
|
|
|
wim_extra_files, wim_nb_files);
|
|
|
|
// Re-use extra files as the final progress step
|
|
|
|
wim_extra_files = (wim_nb_files - wim_proc_files) / 3;
|
|
|
|
UpdateProgressWithInfo(OP_FILE_COPY, MSG_267, wim_proc_files + wim_extra_files, wim_nb_files);
|
2015-01-15 01:45:10 +00:00
|
|
|
r = TRUE;
|
|
|
|
|
|
|
|
out:
|
|
|
|
if ((hImage != NULL) || (hWim != NULL)) {
|
|
|
|
uprintf("Closing: %s", _image);
|
|
|
|
if (hImage != NULL) pfWIMCloseHandle(hImage);
|
|
|
|
if (hWim != NULL) pfWIMCloseHandle(hWim);
|
|
|
|
}
|
2015-01-23 02:26:41 +00:00
|
|
|
if (pfWIMUnregisterMessageCallback != NULL)
|
|
|
|
pfWIMUnregisterMessageCallback(NULL, (FARPROC)WimProgressCallback);
|
2015-01-15 01:45:10 +00:00
|
|
|
safe_free(wimage);
|
|
|
|
safe_free(wdst);
|
|
|
|
ExitThread((DWORD)r);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WimApplyImage(const char* image, int index, const char* dst)
|
|
|
|
{
|
2015-01-16 01:51:24 +00:00
|
|
|
DWORD dw = 0;
|
2015-01-15 01:45:10 +00:00
|
|
|
_image = image;
|
|
|
|
_index = index;
|
|
|
|
_dst = dst;
|
|
|
|
|
2021-10-08 18:22:33 +00:00
|
|
|
wim_thread = CreateThread(NULL, 0, WimApplyImageThread, NULL, 0, NULL);
|
|
|
|
if (wim_thread == NULL) {
|
2015-01-15 01:45:10 +00:00
|
|
|
uprintf("Unable to start apply-image thread");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2021-10-08 18:22:33 +00:00
|
|
|
SetThreadPriority(wim_thread, default_thread_priority);
|
|
|
|
WaitForSingleObject(wim_thread, INFINITE);
|
|
|
|
if (!GetExitCodeThread(wim_thread, &dw))
|
2020-07-10 10:55:29 +00:00
|
|
|
dw = 0;
|
2021-10-08 18:22:33 +00:00
|
|
|
wim_thread = NULL;
|
2020-07-10 10:55:29 +00:00
|
|
|
return dw;
|
2013-01-20 22:46:11 +00:00
|
|
|
}
|