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
|
2012-02-20 13:14:00 +00:00
|
|
|
* Copyright 2012 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;
|
2012-01-12 02:52:40 +00:00
|
|
|
unsigned int syslinux_ldlinux_len;
|
2013-01-13 22:16:10 +00:00
|
|
|
unsigned char* syslinux_bootsect = NULL;
|
2012-01-12 02:52:40 +00:00
|
|
|
unsigned int syslinux_bootsect_len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
2012-01-12 11:04:03 +00:00
|
|
|
BOOL InstallSyslinux(DWORD num, const char* drive_name)
|
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;
|
|
|
|
HGLOBAL res_handle;
|
|
|
|
HRSRC res;
|
|
|
|
|
|
|
|
static unsigned char sectbuf[SECTOR_SIZE];
|
|
|
|
static char ldlinux_name[] = "?:\\ldlinux.sys";
|
|
|
|
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;
|
2012-12-16 23:08:56 +00:00
|
|
|
int dt = (int)ComboBox_GetItemData(hDOSType, ComboBox_GetCurSel(hDOSType));
|
2012-01-12 02:52:40 +00:00
|
|
|
|
|
|
|
ldlinux_name[0] = drive_name[0];
|
|
|
|
|
|
|
|
/* Initialize the ADV -- this should be smarter */
|
|
|
|
syslinux_reset_adv(syslinux_adv);
|
|
|
|
|
|
|
|
/* Access ldlinux.sys resource */
|
|
|
|
res = FindResource(hMainInstance, MAKEINTRESOURCE(IDR_SL_LDLINUX_SYS), RT_RCDATA);
|
|
|
|
if (res == NULL) {
|
|
|
|
uprintf("Unable to locate ldlinux.sys resource: %s\n", WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
res_handle = LoadResource(NULL, res);
|
|
|
|
if (res_handle == NULL) {
|
|
|
|
uprintf("Unable to load ldlinux.sys resource: %s\n", WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
syslinux_ldlinux_len = SizeofResource(NULL, res);
|
2013-01-13 22:16:10 +00:00
|
|
|
syslinux_ldlinux = (unsigned char*)malloc(syslinux_ldlinux_len);
|
|
|
|
if (syslinux_ldlinux == NULL) {
|
|
|
|
uprintf("Unable to allocate ldlinux.sys resource\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
memcpy(syslinux_ldlinux, LockResource(res_handle), syslinux_ldlinux_len);
|
2012-01-12 02:52:40 +00:00
|
|
|
|
|
|
|
/* Access ldlinux.bss resource */
|
|
|
|
res = FindResource(hMainInstance, MAKEINTRESOURCE(IDR_SL_LDLINUX_BSS), RT_RCDATA);
|
|
|
|
if (res == NULL) {
|
|
|
|
uprintf("Unable to locate ldlinux.bss resource: %s\n", WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
res_handle = LoadResource(NULL, res);
|
|
|
|
if (res_handle == NULL) {
|
|
|
|
uprintf("Unable to load ldlinux.bss resource: %s\n", WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
syslinux_bootsect_len = SizeofResource(NULL, res);
|
2013-01-13 22:16:10 +00:00
|
|
|
syslinux_bootsect = (unsigned char*)malloc(syslinux_bootsect_len);
|
|
|
|
if (syslinux_bootsect == NULL) {
|
|
|
|
uprintf("Unable to allocate ldlinux.bss resource\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
memcpy(syslinux_bootsect, LockResource(res_handle), syslinux_bootsect_len);
|
2012-01-12 02:52:40 +00:00
|
|
|
|
|
|
|
/* Create ldlinux.sys file */
|
|
|
|
f_handle = CreateFileA(ldlinux_name, GENERIC_READ | GENERIC_WRITE,
|
|
|
|
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) {
|
|
|
|
uprintf("Unable to create ldlinux.sys\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write ldlinux.sys file */
|
|
|
|
if (!WriteFile(f_handle, syslinux_ldlinux, syslinux_ldlinux_len,
|
|
|
|
&bytes_written, NULL) ||
|
|
|
|
bytes_written != syslinux_ldlinux_len) {
|
|
|
|
uprintf("Could not write ldlinux.sys\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (!WriteFile(f_handle, syslinux_adv, 2 * ADV_SIZE,
|
|
|
|
&bytes_written, NULL) ||
|
|
|
|
bytes_written != 2 * ADV_SIZE) {
|
|
|
|
uprintf("Could not write ADV to ldlinux.sys\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-01-12 11:04:03 +00:00
|
|
|
uprintf("Succesfully wrote 'ldlinux.sys'\n");
|
2012-12-16 23:08:56 +00:00
|
|
|
if (dt == DT_SYSLINUX)
|
|
|
|
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) */
|
|
|
|
d_handle = GetDriveHandle(num, (char*)drive_name, TRUE, FALSE);
|
|
|
|
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);
|
|
|
|
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) {
|
|
|
|
uprintf("Could not write ldlinux.sys: %s\n", WindowsErrorString());
|
|
|
|
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");
|
2012-12-16 23:08:56 +00:00
|
|
|
if (dt == DT_SYSLINUX)
|
|
|
|
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;
|
|
|
|
}
|