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
|
2013-02-01 22:59:46 +00:00
|
|
|
* Copyright 2012-2013 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"
|
|
|
|
#include "resource.h"
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
2013-01-13 22:16:10 +00:00
|
|
|
unsigned char* syslinux_ldlinux = NULL;
|
2013-01-25 01:38:10 +00:00
|
|
|
DWORD syslinux_ldlinux_len;
|
2013-01-13 22:16:10 +00:00
|
|
|
unsigned char* syslinux_bootsect = NULL;
|
2013-01-25 01:38:10 +00:00
|
|
|
DWORD syslinux_bootsect_len;
|
2012-01-12 02:52:40 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Wrapper for ReadFile suitable for libfat
|
|
|
|
*/
|
|
|
|
int libfat_readfile(intptr_t pp, void *buf, size_t secsize,
|
|
|
|
libfat_sector_t sector)
|
|
|
|
{
|
|
|
|
uint64_t offset = (uint64_t) sector * secsize;
|
|
|
|
LONG loword = (LONG) offset;
|
|
|
|
LONG hiword = (LONG) (offset >> 32);
|
|
|
|
LONG hiwordx = hiword;
|
|
|
|
DWORD bytes_read;
|
|
|
|
|
|
|
|
if (SetFilePointer((HANDLE) pp, loword, &hiwordx, FILE_BEGIN) != loword ||
|
|
|
|
hiword != hiwordx ||
|
|
|
|
!ReadFile((HANDLE) pp, buf, (DWORD)secsize, &bytes_read, NULL) ||
|
|
|
|
bytes_read != secsize) {
|
|
|
|
uprintf("Cannot read sector %u\n", sector);
|
|
|
|
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
|
|
|
|
*/
|
2013-04-07 23:10:58 +00:00
|
|
|
BOOL InstallSyslinux(DWORD drive_index, char drive_letter)
|
2012-01-12 02:52:40 +00:00
|
|
|
{
|
|
|
|
HANDLE f_handle = INVALID_HANDLE_VALUE;
|
|
|
|
HANDLE d_handle = INVALID_HANDLE_VALUE;
|
|
|
|
DWORD bytes_read;
|
|
|
|
DWORD bytes_written;
|
|
|
|
BOOL r = FALSE;
|
|
|
|
|
|
|
|
static unsigned char sectbuf[SECTOR_SIZE];
|
2013-06-14 00:55:48 +00:00
|
|
|
static LPSTR resource[2][2] = {
|
|
|
|
{ MAKEINTRESOURCEA(IDR_SL_LDLINUX_V4_SYS), MAKEINTRESOURCEA(IDR_SL_LDLINUX_V4_BSS) },
|
|
|
|
{ MAKEINTRESOURCEA(IDR_SL_LDLINUX_V5_SYS), MAKEINTRESOURCEA(IDR_SL_LDLINUX_V5_BSS) } };
|
|
|
|
static char ldlinux_path[] = "?:\\ldlinux.sys";
|
|
|
|
static char* ldlinux_sys = &ldlinux_path[3];
|
2012-01-12 02:52:40 +00:00
|
|
|
struct libfat_filesystem *fs;
|
|
|
|
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;
|
|
|
|
int nsectors;
|
2013-01-24 21:30:11 +00:00
|
|
|
int dt = (int)ComboBox_GetItemData(hBootType, ComboBox_GetCurSel(hBootType));
|
2013-06-14 00:55:48 +00:00
|
|
|
BOOL use_v5 = (dt == DT_SYSLINUX_V5) || ((dt == DT_ISO) && (iso_report.has_syslinux_v5));
|
2012-01-12 02:52:40 +00:00
|
|
|
|
2013-06-14 00:55:48 +00:00
|
|
|
PrintStatus(0, TRUE, "Installing Syslinux v%d...", use_v5?5:4);
|
|
|
|
|
|
|
|
ldlinux_path[0] = drive_letter;
|
2012-01-12 02:52:40 +00:00
|
|
|
|
|
|
|
/* Initialize the ADV -- this should be smarter */
|
|
|
|
syslinux_reset_adv(syslinux_adv);
|
|
|
|
|
2013-01-25 01:38:10 +00:00
|
|
|
/* Access a copy of the ldlinux.sys & ldlinux.bss resources */
|
2013-06-14 00:55:48 +00:00
|
|
|
syslinux_ldlinux = GetResource(hMainInstance, resource[use_v5?1:0][0],
|
|
|
|
_RT_RCDATA, ldlinux_sys, &syslinux_ldlinux_len, TRUE);
|
|
|
|
syslinux_bootsect = GetResource(hMainInstance, resource[use_v5?1:0][1],
|
|
|
|
_RT_RCDATA, "ldlinux.bss", &syslinux_bootsect_len, TRUE);
|
2013-01-25 01:38:10 +00:00
|
|
|
if ((syslinux_ldlinux == NULL) || (syslinux_bootsect == NULL)) {
|
2012-01-12 02:52:40 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create ldlinux.sys file */
|
2013-06-14 00:55:48 +00:00
|
|
|
f_handle = CreateFileA(ldlinux_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) {
|
2013-06-14 00:55:48 +00:00
|
|
|
uprintf("Unable to create '%s'\n", ldlinux_sys);
|
2012-01-12 02:52:40 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write ldlinux.sys file */
|
|
|
|
if (!WriteFile(f_handle, syslinux_ldlinux, syslinux_ldlinux_len,
|
|
|
|
&bytes_written, NULL) ||
|
|
|
|
bytes_written != syslinux_ldlinux_len) {
|
2013-06-14 00:55:48 +00:00
|
|
|
uprintf("Could not write '%s'\n", ldlinux_sys);
|
2012-01-12 02:52:40 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (!WriteFile(f_handle, syslinux_adv, 2 * ADV_SIZE,
|
|
|
|
&bytes_written, NULL) ||
|
|
|
|
bytes_written != 2 * ADV_SIZE) {
|
2013-06-14 00:55:48 +00:00
|
|
|
uprintf("Could not write ADV to '%s'\n", ldlinux_sys);
|
2012-01-12 02:52:40 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-06-14 00:55:48 +00:00
|
|
|
uprintf("Succesfully wrote '%s'\n", ldlinux_sys);
|
|
|
|
if (dt != DT_ISO)
|
2012-12-16 23:08:56 +00:00
|
|
|
UpdateProgress(OP_DOS, -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)) {
|
|
|
|
uprintf("FlushFileBuffers failed\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reopen the volume (we already have a lock) */
|
2013-04-07 23:10:58 +00:00
|
|
|
d_handle = GetLogicalHandle(drive_index, TRUE, FALSE);
|
2012-01-12 02:52:40 +00:00
|
|
|
if (d_handle == INVALID_HANDLE_VALUE) {
|
|
|
|
uprintf("Could open volume for syslinux operation\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Map the file (is there a better way to do this?) */
|
|
|
|
ldlinux_sectors = (syslinux_ldlinux_len + 2 * ADV_SIZE + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
|
|
|
|
sectors = (libfat_sector_t*) calloc(ldlinux_sectors, sizeof *sectors);
|
2013-01-20 23:34:13 +00:00
|
|
|
if (sectors == NULL)
|
|
|
|
goto out;
|
2012-01-12 02:52:40 +00:00
|
|
|
fs = libfat_open(libfat_readfile, (intptr_t) d_handle);
|
|
|
|
ldlinux_cluster = libfat_searchdir(fs, 0, "LDLINUX SYS", NULL);
|
|
|
|
secp = sectors;
|
|
|
|
nsectors = 0;
|
|
|
|
s = libfat_clustertosector(fs, ldlinux_cluster);
|
|
|
|
while (s && nsectors < ldlinux_sectors) {
|
|
|
|
*secp++ = s;
|
|
|
|
nsectors++;
|
|
|
|
s = libfat_nextsector(fs, s);
|
|
|
|
}
|
|
|
|
libfat_close(fs);
|
|
|
|
|
|
|
|
/* Patch ldlinux.sys and the boot sector */
|
|
|
|
syslinux_patch(sectors, nsectors, 0, 0, NULL, NULL);
|
|
|
|
|
|
|
|
/* Rewrite the file */
|
|
|
|
if (SetFilePointer(f_handle, 0, NULL, FILE_BEGIN) != 0 ||
|
|
|
|
!WriteFile(f_handle, syslinux_ldlinux, syslinux_ldlinux_len,
|
|
|
|
&bytes_written, NULL)
|
|
|
|
|| bytes_written != syslinux_ldlinux_len) {
|
2013-06-14 00:55:48 +00:00
|
|
|
uprintf("Could not write '%s': %s\n", ldlinux_sys, 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 */
|
2012-01-12 02:52:40 +00:00
|
|
|
if (SetFilePointer(d_handle, 0, NULL, FILE_BEGIN) != 0 ||
|
|
|
|
!ReadFile(d_handle, sectbuf, SECTOR_SIZE,
|
|
|
|
&bytes_read, NULL)
|
|
|
|
|| bytes_read != SECTOR_SIZE) {
|
|
|
|
uprintf("Could not read boot record: %s\n", WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-01-12 18:55:30 +00:00
|
|
|
/* Make the syslinux boot sector */
|
2012-02-14 01:23:42 +00:00
|
|
|
syslinux_make_bootsect(sectbuf, VFAT);
|
2012-01-12 02:52:40 +00:00
|
|
|
|
2012-01-12 18:55:30 +00:00
|
|
|
/* Write boot sector back */
|
2012-01-12 02:52:40 +00:00
|
|
|
if (SetFilePointer(d_handle, 0, NULL, FILE_BEGIN) != 0 ||
|
|
|
|
!WriteFile(d_handle, sectbuf, SECTOR_SIZE,
|
|
|
|
&bytes_written, NULL)
|
|
|
|
|| bytes_written != SECTOR_SIZE) {
|
|
|
|
uprintf("Could not write boot record: %s\n", WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-01-12 11:04:03 +00:00
|
|
|
uprintf("Succesfully wrote Syslinux boot record\n");
|
2013-06-14 00:55:48 +00:00
|
|
|
if (dt != DT_ISO)
|
2012-12-16 23:08:56 +00:00
|
|
|
UpdateProgress(OP_DOS, -1.0f);
|
2012-01-12 11:04:03 +00:00
|
|
|
|
2012-01-12 02:52:40 +00:00
|
|
|
r = TRUE;
|
|
|
|
|
|
|
|
out:
|
2013-01-13 22:16:10 +00:00
|
|
|
safe_free(syslinux_ldlinux);
|
|
|
|
safe_free(syslinux_bootsect);
|
2012-01-12 11:04:03 +00:00
|
|
|
safe_free(sectors);
|
2012-01-12 02:52:40 +00:00
|
|
|
safe_closehandle(d_handle);
|
|
|
|
safe_closehandle(f_handle);
|
|
|
|
return r;
|
|
|
|
}
|