2012-01-12 02:52:40 +00:00
|
|
|
/* ----------------------------------------------------------------------- *
|
|
|
|
*
|
|
|
|
* Copyright 2003 Lars Munch Christensen - All Rights Reserved
|
|
|
|
* Copyright 1998-2008 H. Peter Anvin - All Rights Reserved
|
2021-05-31 15:54:11 +00:00
|
|
|
* Copyright 2012-2021 Pete Batard
|
2012-01-12 02:52:40 +00:00
|
|
|
*
|
|
|
|
* Based on the Linux installer program for SYSLINUX by H. Peter Anvin
|
|
|
|
*
|
|
|
|
* 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, Inc., 53 Temple Place Ste 330,
|
|
|
|
* Boston MA 02111-1307, USA; either version 2 of the License, or
|
|
|
|
* (at your option) any later version; incorporated herein by reference.
|
|
|
|
*
|
|
|
|
* ----------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* Memory leaks detection - define _CRTDBG_MAP_ALLOC as preprocessor macro */
|
|
|
|
#ifdef _CRTDBG_MAP_ALLOC
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <crtdbg.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <windows.h>
|
2012-12-16 23:08:56 +00:00
|
|
|
#include <windowsx.h>
|
2012-01-12 02:52:40 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "rufus.h"
|
2016-02-24 16:10:54 +00:00
|
|
|
#include "missing.h"
|
2012-01-12 02:52:40 +00:00
|
|
|
#include "resource.h"
|
2014-01-21 17:08:41 +00:00
|
|
|
#include "msapi_utf8.h"
|
2016-02-20 22:52:32 +00:00
|
|
|
#include "localization.h"
|
|
|
|
|
|
|
|
#include "drive.h"
|
2012-01-12 02:52:40 +00:00
|
|
|
|
|
|
|
#include "syslinux.h"
|
2012-02-14 01:23:42 +00:00
|
|
|
#include "syslxfs.h"
|
2012-01-12 02:52:40 +00:00
|
|
|
#include "libfat.h"
|
|
|
|
#include "setadv.h"
|
2014-11-11 19:53:39 +00:00
|
|
|
#include "ntfssect.h"
|
2012-01-12 02:52:40 +00:00
|
|
|
|
2014-01-21 17:08:41 +00:00
|
|
|
unsigned char* syslinux_ldlinux[2] = { NULL, NULL };
|
2016-02-23 20:52:06 +00:00
|
|
|
unsigned long syslinux_ldlinux_len[2];
|
2014-01-02 23:21:30 +00:00
|
|
|
unsigned char* syslinux_mboot = NULL;
|
2016-02-23 20:52:06 +00:00
|
|
|
unsigned long syslinux_mboot_len;
|
2012-01-12 02:52:40 +00:00
|
|
|
|
2016-06-10 11:42:43 +00:00
|
|
|
/* Workaround for 4K support */
|
2015-06-02 20:47:44 +00:00
|
|
|
uint32_t SECTOR_SHIFT = 9;
|
|
|
|
uint32_t SECTOR_SIZE = 512;
|
|
|
|
uint32_t LIBFAT_SECTOR_SHIFT = 9;
|
|
|
|
uint32_t LIBFAT_SECTOR_SIZE = 512;
|
|
|
|
uint32_t LIBFAT_SECTOR_MASK = 511;
|
|
|
|
|
2012-01-12 02:52:40 +00:00
|
|
|
/*
|
|
|
|
* Wrapper for ReadFile suitable for libfat
|
|
|
|
*/
|
2016-06-06 18:39:53 +00:00
|
|
|
int libfat_readfile(intptr_t pp, void *buf, size_t secsize, libfat_sector_t sector)
|
2012-01-12 02:52:40 +00:00
|
|
|
{
|
2016-06-06 18:39:53 +00:00
|
|
|
LARGE_INTEGER offset;
|
2012-01-12 02:52:40 +00:00
|
|
|
DWORD bytes_read;
|
|
|
|
|
2016-06-06 18:39:53 +00:00
|
|
|
offset.QuadPart = (LONGLONG) sector * secsize;
|
|
|
|
if (!SetFilePointerEx((HANDLE) pp, offset, NULL, FILE_BEGIN)) {
|
|
|
|
uprintf("Could not set pointer to position %llu: %s", offset.QuadPart, WindowsErrorString());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ReadFile((HANDLE) pp, buf, (DWORD) secsize, &bytes_read, NULL)) {
|
|
|
|
uprintf("Could not read sector %llu: %s", sector, WindowsErrorString());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bytes_read != secsize) {
|
|
|
|
uprintf("Sector %llu: Read %d bytes instead of %d requested", sector, bytes_read, secsize);
|
2012-01-12 02:52:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int)secsize;
|
|
|
|
}
|
|
|
|
|
2012-01-12 18:55:30 +00:00
|
|
|
/*
|
|
|
|
* Extract the ldlinux.sys and ldlinux.bss from resources,
|
|
|
|
* then patch and install them
|
|
|
|
*/
|
2019-04-13 14:29:27 +00:00
|
|
|
BOOL InstallSyslinux(DWORD drive_index, char drive_letter, int file_system)
|
2012-01-12 02:52:40 +00:00
|
|
|
{
|
2016-06-06 18:39:53 +00:00
|
|
|
const LARGE_INTEGER liZero = { {0, 0} };
|
2012-01-12 02:52:40 +00:00
|
|
|
HANDLE f_handle = INVALID_HANDLE_VALUE;
|
|
|
|
HANDLE d_handle = INVALID_HANDLE_VALUE;
|
2015-02-26 00:15:13 +00:00
|
|
|
DWORD bytes_read, bytes_written, err;
|
2017-02-17 11:05:49 +00:00
|
|
|
S_NTFSSECT_VOLINFO vol_info = { 0 };
|
2015-02-26 00:15:13 +00:00
|
|
|
LARGE_INTEGER vcn, lba, len;
|
|
|
|
S_NTFSSECT_EXTENT extent;
|
2012-01-12 02:52:40 +00:00
|
|
|
BOOL r = FALSE;
|
2013-06-15 16:31:55 +00:00
|
|
|
FILE* fd;
|
2014-11-11 19:53:39 +00:00
|
|
|
size_t length;
|
2012-01-12 02:52:40 +00:00
|
|
|
|
2015-06-02 20:47:44 +00:00
|
|
|
static unsigned char* sectbuf = NULL;
|
2014-01-02 23:21:30 +00:00
|
|
|
static char* resource[2][2] = {
|
|
|
|
{ MAKEINTRESOURCEA(IDR_SL_LDLINUX_V4_SYS), MAKEINTRESOURCEA(IDR_SL_LDLINUX_V4_BSS) },
|
2014-11-11 19:53:39 +00:00
|
|
|
{ MAKEINTRESOURCEA(IDR_SL_LDLINUX_V6_SYS), MAKEINTRESOURCEA(IDR_SL_LDLINUX_V6_BSS) } };
|
2014-01-21 17:08:41 +00:00
|
|
|
const char* ldlinux = "ldlinux";
|
|
|
|
const char* syslinux = "syslinux";
|
|
|
|
const char* ldlinux_ext[3] = { "sys", "bss", "c32" };
|
2014-01-02 23:21:30 +00:00
|
|
|
const char* mboot_c32 = "mboot.c32";
|
2014-01-21 17:08:41 +00:00
|
|
|
char path[MAX_PATH], tmp[64];
|
2016-06-09 16:49:58 +00:00
|
|
|
const char *errmsg;
|
2018-03-22 23:14:20 +00:00
|
|
|
struct libfat_filesystem *lf_fs;
|
2012-01-12 02:52:40 +00:00
|
|
|
libfat_sector_t s, *secp;
|
2012-01-12 11:04:03 +00:00
|
|
|
libfat_sector_t *sectors = NULL;
|
2012-01-12 02:52:40 +00:00
|
|
|
int ldlinux_sectors;
|
|
|
|
uint32_t ldlinux_cluster;
|
2016-06-09 16:49:58 +00:00
|
|
|
int i, nsectors, sl_fs_stype;
|
2019-04-13 14:29:27 +00:00
|
|
|
BOOL use_v5 = (boot_type == BT_SYSLINUX_V6) || ((boot_type == BT_IMAGE) && (SL_MAJOR(img_report.sl_version) >= 5));
|
2012-01-12 02:52:40 +00:00
|
|
|
|
2019-04-13 14:29:27 +00:00
|
|
|
PrintInfoDebug(0, MSG_234, (boot_type == BT_IMAGE)?img_report.sl_version_str:embedded_sl_version_str[use_v5?1:0]);
|
2012-01-12 02:52:40 +00:00
|
|
|
|
2016-06-09 16:49:58 +00:00
|
|
|
/* 4K sector size workaround */
|
2015-06-02 20:47:44 +00:00
|
|
|
SECTOR_SHIFT = 0;
|
2016-05-23 11:19:11 +00:00
|
|
|
SECTOR_SIZE = SelectedDrive.SectorSize;
|
2015-06-02 20:47:44 +00:00
|
|
|
while (SECTOR_SIZE>>=1)
|
|
|
|
SECTOR_SHIFT++;
|
2016-05-23 11:19:11 +00:00
|
|
|
SECTOR_SIZE = SelectedDrive.SectorSize;
|
2015-06-02 20:47:44 +00:00
|
|
|
LIBFAT_SECTOR_SHIFT = SECTOR_SHIFT;
|
|
|
|
LIBFAT_SECTOR_SIZE = SECTOR_SIZE;
|
|
|
|
LIBFAT_SECTOR_MASK = SECTOR_SIZE - 1;
|
|
|
|
|
2016-06-09 16:49:58 +00:00
|
|
|
/* sectbuf should be aligned to at least 8 bytes - see github #767 */
|
2016-06-10 11:42:43 +00:00
|
|
|
sectbuf = _mm_malloc(SECTOR_SIZE, 16);
|
2015-06-02 20:47:44 +00:00
|
|
|
if (sectbuf == NULL)
|
|
|
|
goto out;
|
2015-10-28 23:17:55 +00:00
|
|
|
|
2017-02-16 14:13:30 +00:00
|
|
|
/*
|
|
|
|
* First, reopen the volume (we already have a lock). Also, for some
|
|
|
|
* weird reason.the Syslinux install process *MUST* have FILE_SHARE_WRITE
|
|
|
|
* on the volume, or else creating 'ldlinux.sys' will fail...
|
|
|
|
*/
|
2019-04-25 17:58:55 +00:00
|
|
|
d_handle = GetLogicalHandle(drive_index, 0, FALSE, TRUE, TRUE);
|
2016-06-09 16:49:58 +00:00
|
|
|
if ((d_handle == INVALID_HANDLE_VALUE) || (d_handle == NULL)) {
|
|
|
|
uprintf("Could open volume for Syslinux installation");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure we can read the boot sector (NB: Re-open already set us to offset 0) */
|
|
|
|
if (!ReadFile(d_handle, sectbuf, SECTOR_SIZE, &bytes_read, NULL)) {
|
|
|
|
uprintf("Could not read VBR");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (bytes_read != SECTOR_SIZE) {
|
|
|
|
uprintf("Could not read the whole VBR");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((errmsg = syslinux_check_bootsect(sectbuf, &sl_fs_stype))) {
|
|
|
|
uprintf("Error: %s", errmsg);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-01-12 02:52:40 +00:00
|
|
|
/* Initialize the ADV -- this should be smarter */
|
|
|
|
syslinux_reset_adv(syslinux_adv);
|
|
|
|
|
2014-01-21 17:08:41 +00:00
|
|
|
/* Access a copy of the ldlinux.sys & ldlinux.bss resources (downloaded or embedded) */
|
|
|
|
if ((syslinux_ldlinux_len[0] != 0) && (syslinux_ldlinux_len[1] != 0)) {
|
2021-05-31 15:54:11 +00:00
|
|
|
IGNORE_RETVAL(_chdirU(app_data_dir));
|
2021-10-21 00:59:03 +00:00
|
|
|
for (i = 0; i < 2; i++) {
|
2014-01-21 17:08:41 +00:00
|
|
|
syslinux_ldlinux[i] = (unsigned char*) malloc(syslinux_ldlinux_len[i]);
|
|
|
|
if (syslinux_ldlinux[i] == NULL)
|
|
|
|
goto out;
|
2021-10-21 00:59:03 +00:00
|
|
|
static_sprintf(path, "%s\\%s-%s%s\\%s.%s", FILES_DIR, syslinux, img_report.sl_version_str,
|
2015-09-02 22:20:00 +00:00
|
|
|
img_report.sl_version_ext, ldlinux, i==0?"sys":"bss");
|
2014-01-21 17:08:41 +00:00
|
|
|
fd = fopen(path, "rb");
|
|
|
|
if (fd == NULL) {
|
2021-10-21 00:59:03 +00:00
|
|
|
uprintf("Could not open %s\\%s", app_data_dir, path);
|
2014-01-21 17:08:41 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2014-11-11 19:53:39 +00:00
|
|
|
length = fread(syslinux_ldlinux[i], 1, (size_t)syslinux_ldlinux_len[i], fd);
|
2014-01-21 17:08:41 +00:00
|
|
|
fclose(fd);
|
2014-11-11 19:53:39 +00:00
|
|
|
if (length != (size_t)syslinux_ldlinux_len[i]) {
|
2021-10-21 00:59:03 +00:00
|
|
|
uprintf("Could not read %s\\%s", app_data_dir, path);
|
2014-01-21 17:08:41 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2021-10-11 19:08:04 +00:00
|
|
|
uprintf("Using existing '%s\\%s' %s", app_data_dir, path,
|
2016-05-25 11:20:20 +00:00
|
|
|
IsBufferInDB(syslinux_ldlinux[i], (size_t)syslinux_ldlinux_len[i])?"✓":"✗");
|
2014-01-21 17:08:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-10-21 00:59:03 +00:00
|
|
|
for (i = 0; i < 2; i++) {
|
2014-01-21 17:08:41 +00:00
|
|
|
static_sprintf(tmp, "%s.%s", ldlinux, ldlinux_ext[i]);
|
2020-02-19 12:41:13 +00:00
|
|
|
syslinux_ldlinux_len[i] = 0;
|
2014-01-21 17:08:41 +00:00
|
|
|
syslinux_ldlinux[i] = GetResource(hMainInstance, resource[use_v5?1:0][i],
|
|
|
|
_RT_RCDATA, tmp, &syslinux_ldlinux_len[i], TRUE);
|
|
|
|
if (syslinux_ldlinux[i] == NULL)
|
|
|
|
goto out;
|
|
|
|
}
|
2012-01-12 02:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create ldlinux.sys file */
|
2022-01-31 16:55:42 +00:00
|
|
|
static_sprintf(path, "%c:\\%s.%s", toupper(drive_letter), ldlinux, ldlinux_ext[0]);
|
2014-01-21 17:08:41 +00:00
|
|
|
f_handle = CreateFileA(path, GENERIC_READ | GENERIC_WRITE,
|
2012-01-12 02:52:40 +00:00
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
NULL, CREATE_ALWAYS,
|
|
|
|
FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM |
|
|
|
|
FILE_ATTRIBUTE_HIDDEN, NULL);
|
|
|
|
|
|
|
|
if (f_handle == INVALID_HANDLE_VALUE) {
|
2017-02-16 14:13:30 +00:00
|
|
|
uprintf("Unable to create '%s': %s", &path[3], WindowsErrorString());
|
2012-01-12 02:52:40 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write ldlinux.sys file */
|
2016-01-14 17:43:02 +00:00
|
|
|
if (!WriteFileWithRetry(f_handle, (const char _force *)syslinux_ldlinux[0],
|
|
|
|
syslinux_ldlinux_len[0], &bytes_written, WRITE_RETRIES)) {
|
|
|
|
uprintf("Could not write '%s': %s", &path[3], WindowsErrorString());
|
2012-01-12 02:52:40 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2016-01-14 17:43:02 +00:00
|
|
|
if (!WriteFileWithRetry(f_handle, syslinux_adv, 2 * ADV_SIZE,
|
|
|
|
&bytes_written, WRITE_RETRIES)) {
|
|
|
|
uprintf("Could not write ADV to '%s': %s", &path[3], WindowsErrorString());
|
2012-01-12 02:52:40 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2016-01-14 17:43:02 +00:00
|
|
|
uprintf("Successfully wrote '%s'", &path[3]);
|
2019-04-13 14:29:27 +00:00
|
|
|
if (boot_type != BT_IMAGE)
|
2019-08-20 17:06:07 +00:00
|
|
|
UpdateProgress(OP_FILE_COPY, -1.0f);
|
2012-01-12 11:04:03 +00:00
|
|
|
|
2012-01-12 02:52:40 +00:00
|
|
|
/* Now flush the media */
|
|
|
|
if (!FlushFileBuffers(f_handle)) {
|
2016-01-14 17:43:02 +00:00
|
|
|
uprintf("FlushFileBuffers failed");
|
2012-01-12 02:52:40 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Map the file (is there a better way to do this?) */
|
2014-01-21 17:08:41 +00:00
|
|
|
ldlinux_sectors = (syslinux_ldlinux_len[0] + 2 * ADV_SIZE + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
|
2012-01-12 02:52:40 +00:00
|
|
|
sectors = (libfat_sector_t*) calloc(ldlinux_sectors, sizeof *sectors);
|
2013-01-20 23:34:13 +00:00
|
|
|
if (sectors == NULL)
|
|
|
|
goto out;
|
2014-11-11 19:53:39 +00:00
|
|
|
|
2019-04-13 14:29:27 +00:00
|
|
|
switch (file_system) {
|
2015-02-26 00:15:13 +00:00
|
|
|
case FS_NTFS:
|
2022-01-31 16:55:42 +00:00
|
|
|
static_sprintf(tmp, "%c:\\", toupper(drive_letter));
|
2015-02-26 00:15:13 +00:00
|
|
|
vol_info.Handle = d_handle;
|
2014-11-11 19:53:39 +00:00
|
|
|
err = NtfsSectGetVolumeInfo(tmp, &vol_info);
|
|
|
|
if (err != ERROR_SUCCESS) {
|
|
|
|
uprintf("Could not fetch NTFS volume info");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
secp = sectors;
|
|
|
|
nsectors = 0;
|
|
|
|
for (vcn.QuadPart = 0;
|
|
|
|
NtfsSectGetFileVcnExtent(f_handle, &vcn, &extent) == ERROR_SUCCESS;
|
|
|
|
vcn = extent.NextVcn) {
|
|
|
|
err = NtfsSectLcnToLba(&vol_info, &extent.FirstLcn, &lba);
|
|
|
|
if (err != ERROR_SUCCESS) {
|
2021-10-21 00:59:03 +00:00
|
|
|
uprintf("Could not translate 'ldlinux.sys' LCN to disk LBA");
|
2014-11-11 19:53:39 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
lba.QuadPart -= vol_info.PartitionLba.QuadPart;
|
|
|
|
len.QuadPart = ((extent.NextVcn.QuadPart -
|
|
|
|
extent.FirstVcn.QuadPart) *
|
|
|
|
vol_info.SectorsPerCluster);
|
|
|
|
while (len.QuadPart-- && nsectors < ldlinux_sectors) {
|
|
|
|
*secp++ = lba.QuadPart++;
|
|
|
|
nsectors++;
|
|
|
|
}
|
|
|
|
}
|
2015-02-26 00:15:13 +00:00
|
|
|
break;
|
|
|
|
case FS_FAT16:
|
|
|
|
case FS_FAT32:
|
|
|
|
case FS_EXFAT:
|
2018-03-22 23:14:20 +00:00
|
|
|
lf_fs = libfat_open(libfat_readfile, (intptr_t) d_handle);
|
|
|
|
if (lf_fs == NULL) {
|
2016-01-14 17:43:02 +00:00
|
|
|
uprintf("Syslinux FAT access error");
|
2015-02-26 00:15:13 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2018-03-22 23:14:20 +00:00
|
|
|
ldlinux_cluster = libfat_searchdir(lf_fs, 0, "LDLINUX SYS", NULL);
|
2015-02-26 00:15:13 +00:00
|
|
|
secp = sectors;
|
|
|
|
nsectors = 0;
|
2018-03-22 23:14:20 +00:00
|
|
|
s = libfat_clustertosector(lf_fs, ldlinux_cluster);
|
2015-02-26 00:15:13 +00:00
|
|
|
while (s && nsectors < ldlinux_sectors) {
|
|
|
|
*secp++ = s;
|
|
|
|
nsectors++;
|
2018-03-22 23:14:20 +00:00
|
|
|
s = libfat_nextsector(lf_fs, s);
|
2015-02-26 00:15:13 +00:00
|
|
|
}
|
2018-03-22 23:14:20 +00:00
|
|
|
libfat_close(lf_fs);
|
2015-02-26 00:15:13 +00:00
|
|
|
break;
|
|
|
|
default:
|
2016-01-14 17:43:02 +00:00
|
|
|
uprintf("Unsupported Syslinux filesystem");
|
2013-07-05 21:26:47 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2012-01-12 02:52:40 +00:00
|
|
|
|
|
|
|
/* Patch ldlinux.sys and the boot sector */
|
2015-10-28 23:17:55 +00:00
|
|
|
if (syslinux_patch(sectors, nsectors, 0, 0, NULL, NULL) < 0) {
|
2016-05-16 15:33:53 +00:00
|
|
|
uprintf("Could not patch Syslinux files.");
|
2021-11-01 16:00:02 +00:00
|
|
|
uprintf("WARNING: This could be caused by your firewall having modified downloaded content, such as 'ldlinux.sys'...");
|
2015-10-28 23:17:55 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2012-01-12 02:52:40 +00:00
|
|
|
|
|
|
|
/* Rewrite the file */
|
2016-06-06 18:39:53 +00:00
|
|
|
if (!SetFilePointerEx(f_handle, liZero, NULL, FILE_BEGIN) ||
|
2016-01-14 17:43:02 +00:00
|
|
|
!WriteFileWithRetry(f_handle, syslinux_ldlinux[0], syslinux_ldlinux_len[0],
|
|
|
|
&bytes_written, WRITE_RETRIES)) {
|
2016-06-06 18:39:53 +00:00
|
|
|
uprintf("Could not rewrite '%s': %s\n", &path[3], WindowsErrorString());
|
2012-01-12 02:52:40 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close file */
|
|
|
|
safe_closehandle(f_handle);
|
|
|
|
|
2012-01-12 18:55:30 +00:00
|
|
|
/* Read existing FAT data into boot sector */
|
2016-06-06 18:39:53 +00:00
|
|
|
if (!SetFilePointerEx(d_handle, liZero, NULL, FILE_BEGIN) ||
|
2012-01-12 02:52:40 +00:00
|
|
|
!ReadFile(d_handle, sectbuf, SECTOR_SIZE,
|
2016-01-14 17:43:02 +00:00
|
|
|
&bytes_read, NULL)) {
|
|
|
|
uprintf("Could not read Syslinux boot record: %s", WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (bytes_read < SECTOR_SIZE) {
|
|
|
|
uprintf("Partial read of Syslinux boot record: read %d bytes but requested %d", bytes_read, SECTOR_SIZE);
|
2012-01-12 02:52:40 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-01-12 18:55:30 +00:00
|
|
|
/* Make the syslinux boot sector */
|
2019-04-13 14:29:27 +00:00
|
|
|
syslinux_make_bootsect(sectbuf, (file_system == FS_NTFS)?NTFS:VFAT);
|
2012-01-12 02:52:40 +00:00
|
|
|
|
2012-01-12 18:55:30 +00:00
|
|
|
/* Write boot sector back */
|
2016-06-06 18:39:53 +00:00
|
|
|
if (!SetFilePointerEx(d_handle, liZero, NULL, FILE_BEGIN) ||
|
2016-01-14 17:43:02 +00:00
|
|
|
!WriteFileWithRetry(d_handle, sectbuf, SECTOR_SIZE,
|
|
|
|
&bytes_written, WRITE_RETRIES)) {
|
|
|
|
uprintf("Could not write Syslinux boot record: %s", WindowsErrorString());
|
2012-01-12 02:52:40 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2016-01-14 17:43:02 +00:00
|
|
|
uprintf("Successfully wrote Syslinux boot record");
|
2013-06-15 16:31:55 +00:00
|
|
|
|
2019-04-13 14:29:27 +00:00
|
|
|
if (boot_type == BT_SYSLINUX_V6) {
|
2021-05-31 15:54:11 +00:00
|
|
|
IGNORE_RETVAL(_chdirU(app_data_dir));
|
2021-10-21 00:59:03 +00:00
|
|
|
static_sprintf(path, "%s\\%s-%s", FILES_DIR, syslinux, embedded_sl_version_str[1]);
|
2014-01-14 20:17:07 +00:00
|
|
|
IGNORE_RETVAL(_chdir(path));
|
2022-01-31 16:55:42 +00:00
|
|
|
static_sprintf(path, "%c:\\%s.%s", toupper(drive_letter), ldlinux, ldlinux_ext[2]);
|
2014-01-21 17:08:41 +00:00
|
|
|
fd = fopen(&path[3], "rb");
|
2013-06-15 16:31:55 +00:00
|
|
|
if (fd == NULL) {
|
2016-01-14 17:43:02 +00:00
|
|
|
uprintf("Caution: No '%s' was provided. The target will be missing a required Syslinux file!", &path[3]);
|
2013-06-15 16:31:55 +00:00
|
|
|
} else {
|
|
|
|
fclose(fd);
|
2016-05-25 11:20:20 +00:00
|
|
|
if (CopyFileU(&path[3], path, TRUE)) {
|
2021-10-21 00:59:03 +00:00
|
|
|
uprintf("Created '%s' (from '%s\\%s\\%s-%s\\%s') %s", path, app_data_dir, FILES_DIR, syslinux,
|
2016-05-25 11:20:20 +00:00
|
|
|
embedded_sl_version_str[1], &path[3], IsFileInDB(&path[3])?"✓":"✗");
|
2013-06-15 16:31:55 +00:00
|
|
|
} else {
|
2016-01-14 17:43:02 +00:00
|
|
|
uprintf("Failed to create '%s': %s", path, WindowsErrorString());
|
2013-06-15 16:31:55 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-14 23:27:04 +00:00
|
|
|
} else if (HAS_REACTOS(img_report)) {
|
2016-01-14 17:43:02 +00:00
|
|
|
uprintf("Setting up ReactOS...");
|
2014-01-02 23:21:30 +00:00
|
|
|
syslinux_mboot = GetResource(hMainInstance, MAKEINTRESOURCEA(IDR_SL_MBOOT_C32),
|
|
|
|
_RT_RCDATA, "mboot.c32", &syslinux_mboot_len, FALSE);
|
|
|
|
if (syslinux_mboot == NULL) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* Create mboot.c32 file */
|
2022-01-31 16:55:42 +00:00
|
|
|
static_sprintf(path, "%c:\\%s", toupper(drive_letter), mboot_c32);
|
2014-01-02 23:21:30 +00:00
|
|
|
f_handle = CreateFileA(path, GENERIC_READ | GENERIC_WRITE,
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
if (f_handle == INVALID_HANDLE_VALUE) {
|
|
|
|
uprintf("Unable to create '%s'\n", path);
|
|
|
|
goto out;
|
|
|
|
}
|
2016-01-14 17:43:02 +00:00
|
|
|
if (!WriteFileWithRetry(f_handle, syslinux_mboot, syslinux_mboot_len,
|
|
|
|
&bytes_written, WRITE_RETRIES)) {
|
|
|
|
uprintf("Could not write '%s'", path);
|
2014-01-02 23:21:30 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
safe_closehandle(f_handle);
|
2022-01-31 16:55:42 +00:00
|
|
|
static_sprintf(path, "%c:\\syslinux.cfg", toupper(drive_letter));
|
2014-01-02 23:21:30 +00:00
|
|
|
fd = fopen(path, "w");
|
|
|
|
if (fd == NULL) {
|
2016-01-14 17:43:02 +00:00
|
|
|
uprintf("Could not create ReactOS 'syslinux.cfg'");
|
2014-01-02 23:21:30 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* Write the syslinux.cfg for ReactOS */
|
2016-01-14 17:43:02 +00:00
|
|
|
fprintf(fd, "DEFAULT ReactOS\nLABEL ReactOS\n KERNEL %s\n APPEND %s",
|
2015-09-02 22:20:00 +00:00
|
|
|
mboot_c32, img_report.reactos_path);
|
2014-01-02 23:21:30 +00:00
|
|
|
fclose(fd);
|
2013-06-15 16:31:55 +00:00
|
|
|
}
|
|
|
|
|
2019-04-13 14:29:27 +00:00
|
|
|
if (boot_type != BT_IMAGE)
|
2019-08-20 17:06:07 +00:00
|
|
|
UpdateProgress(OP_FILE_COPY, -1.0f);
|
2012-01-12 11:04:03 +00:00
|
|
|
|
2012-01-12 02:52:40 +00:00
|
|
|
r = TRUE;
|
|
|
|
|
|
|
|
out:
|
2016-06-10 11:42:43 +00:00
|
|
|
safe_mm_free(sectbuf);
|
2014-01-21 17:08:41 +00:00
|
|
|
safe_free(syslinux_ldlinux[0]);
|
|
|
|
safe_free(syslinux_ldlinux[1]);
|
2012-01-12 11:04:03 +00:00
|
|
|
safe_free(sectors);
|
2017-02-16 14:13:30 +00:00
|
|
|
safe_unlockclose(d_handle);
|
2012-01-12 02:52:40 +00:00
|
|
|
safe_closehandle(f_handle);
|
|
|
|
return r;
|
|
|
|
}
|
2014-08-05 23:57:32 +00:00
|
|
|
|
|
|
|
uint16_t GetSyslinuxVersion(char* buf, size_t buf_size, char** ext)
|
|
|
|
{
|
2014-11-20 23:00:59 +00:00
|
|
|
size_t i, j, k;
|
2014-08-05 23:57:32 +00:00
|
|
|
char *p;
|
|
|
|
uint16_t version;
|
|
|
|
const char LINUX[] = { 'L', 'I', 'N', 'U', 'X', ' ' };
|
|
|
|
static char* nullstr = "";
|
2014-11-20 23:00:59 +00:00
|
|
|
char unauthorized[] = {'<', '>', ':', '|', '*', '?', '\\', '/'};
|
2014-08-05 23:57:32 +00:00
|
|
|
|
|
|
|
*ext = nullstr;
|
|
|
|
if (buf_size < 256)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Start at 64 to avoid the short incomplete version at the beginning of ldlinux.sys
|
|
|
|
for (i=64; i<buf_size-64; i++) {
|
|
|
|
if (memcmp(&buf[i], LINUX, sizeof(LINUX)) == 0) {
|
|
|
|
// Check for ISO or SYS prefix
|
|
|
|
if (!( ((buf[i-3] == 'I') && (buf[i-2] == 'S') && (buf[i-1] == 'O'))
|
|
|
|
|| ((buf[i-3] == 'S') && (buf[i-2] == 'Y') && (buf[i-1] == 'S')) ))
|
|
|
|
continue;
|
|
|
|
i += sizeof(LINUX);
|
|
|
|
version = (((uint8_t)strtoul(&buf[i], &p, 10))<<8) + (uint8_t)strtoul(&p[1], &p, 10);
|
|
|
|
if (version == 0)
|
|
|
|
continue;
|
|
|
|
p[safe_strlen(p)] = 0;
|
|
|
|
// Ensure that our extra version string starts with a slash
|
|
|
|
*p = '/';
|
|
|
|
// Remove the x.yz- duplicate if present
|
|
|
|
for (j=0; (buf[i+j] == p[1+j]) && (buf[i+j] != ' '); j++);
|
|
|
|
if (p[j+1] == '-')
|
|
|
|
j++;
|
|
|
|
if (j >= 4) {
|
|
|
|
p[j] = '/';
|
|
|
|
p = &p[j];
|
|
|
|
}
|
|
|
|
for (j=safe_strlen(p)-1; j>0; j--) {
|
|
|
|
// Arch Linux affixes a star for their version - who knows what else is out there...
|
|
|
|
if ((p[j] == ' ') || (p[j] == '*'))
|
|
|
|
p[j] = 0;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Sanitize the string
|
2014-11-20 23:00:59 +00:00
|
|
|
for (j=1; j<safe_strlen(p); j++) {
|
|
|
|
// Some people are bound to have invalid chars in their date strings
|
|
|
|
for (k=0; k<sizeof(unauthorized); k++) {
|
|
|
|
if (p[j] == unauthorized[k])
|
|
|
|
p[j] = '_';
|
|
|
|
}
|
|
|
|
}
|
2014-08-05 23:57:32 +00:00
|
|
|
// If all we have is a slash, return the empty string for the extra version
|
|
|
|
*ext = (p[1] == 0)?nullstr:p;
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|