summaryrefslogtreecommitdiff
path: root/win32/syslinux.c
blob: 2f614b4e912e426b177bb2286f1de7de64e94a2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* ----------------------------------------------------------------------- *
 *   
 *   Copyright 2003 Lars Munch Christensen - All Rights Reserved
 *	
 *   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.
 *
 * ----------------------------------------------------------------------- */

/*
 * syslinux-mingw.c - Win2k/WinXP installer program for SYSLINUX
 */

#include <windows.h>
#include <stdio.h>
#include <ctype.h>

#include "syslinux.h"

char *program;			/* Name of program */
char *drive;			/* Drive to install to */

/*
 * Check Windows version.
 *
 * On Windows Me/98/95 you cannot open a directory, physical disk, or
 * volume using CreateFile.
 */
int checkver()
{
  OSVERSIONINFO osvi;

  osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  GetVersionEx(&osvi);

  return  (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) &&
          ((osvi.dwMajorVersion > 4) ||
          ((osvi.dwMajorVersion == 4) && (osvi.dwMinorVersion == 0)));
}

/*
 * Windows error function
 */
void error(char* msg)
{
  LPVOID lpMsgBuf;

  /* Format the Windows error message */
  FormatMessage( 
		FORMAT_MESSAGE_ALLOCATE_BUFFER | 
		FORMAT_MESSAGE_FROM_SYSTEM | 
		FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL, GetLastError(),
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
		(LPTSTR) &lpMsgBuf, 0, NULL );
  
  /* Print it */
  fprintf(stderr, "%s: %s", msg, (char*) lpMsgBuf);

  /* Free the buffer */
  LocalFree(lpMsgBuf);
}

void usage(void)
{
  fprintf(stderr, "Usage: syslinux.exe [-sf] <drive>:\n");
  exit(1);
}

int main(int argc, char *argv[])
{
  HANDLE f_handle;
  DWORD bytes_read;
  DWORD bytes_written;
  DWORD drives;
  UINT drive_type;

  static unsigned char sectbuf[512];
  char **argp, *opt;
  char drive_name[128];
  char ldlinux_name[128];
  const char *errmsg;

  int force = 0;		/* -f (force) option */

  (void)argc;

  if (!checkver()) {
    fprintf(stderr, "You need to be running at least Windows NT\n");
    exit(1);	    
  }

  program = argv[0];
  drive = NULL;

  for ( argp = argv+1 ; *argp ; argp++ ) {
    if ( **argp == '-' ) {
      opt = *argp + 1;
      if ( !*opt )
	usage();

      while ( *opt ) {
	if ( *opt == 's' ) {
	  syslinux_make_stupid();	/* Use "safe, slow and stupid" code */
        } else if ( *opt == 'f' ) {
          force = 1;                    /* Force install */
	} else {
	  usage();
	}
	opt++;
      }
    } else {
      if ( drive )
	usage();
      drive = *argp;
    }
  }

  if ( !drive )
    usage();

  /* Test if drive exists */
  drives = GetLogicalDrives();
  if(!(drives & ( 1 << (tolower(drive[0]) - 'a')))) {
    fprintf(stderr, "No such drive %c:\n", drive[0]);
    exit(1);
  }

  /* Determines the drive type */
  sprintf(drive_name, "%c:\\", drive[0]);
  drive_type = GetDriveType(drive_name);

  /* Test for removeable media */
  if ((drive_type == DRIVE_FIXED) && (force == 0)) {
    fprintf(stderr, "Not a removable drive (use -f to override) \n");
    exit(1);
  }

  /* Test for unsupported media */
  if ((drive_type != DRIVE_FIXED) && (drive_type != DRIVE_REMOVABLE)) {
    fprintf(stderr, "Unsupported media\n");
    exit(1);
  }

  /*
   * First open the drive
   */
  sprintf(drive_name, "\\\\.\\%c:", drive[0]);
  f_handle = CreateFile(drive_name, GENERIC_READ | GENERIC_WRITE,
			 FILE_SHARE_READ | FILE_SHARE_WRITE,
			 NULL, OPEN_EXISTING, 0, NULL );

  if(f_handle == INVALID_HANDLE_VALUE) {
    error("Could not open drive");
    exit(1);
  }

  /*
   * Read the boot sector
   */  
  ReadFile(f_handle, sectbuf, 512, &bytes_read, NULL);
  if(bytes_read != 512) {
    fprintf(stderr, "Could not read the whole boot sector\n");
    exit(1);
  }
  
  /* Check to see that what we got was indeed an MS-DOS boot sector/superblock */
  if(!syslinux_check_bootsect(sectbuf,&errmsg)) {
    fprintf(stderr, "%s\n", errmsg);
    exit(1);
  }

  /* Make the syslinux boot sector */
  syslinux_make_bootsect(sectbuf);

  /* Write the syslinux boot sector into the boot sector */
  SetFilePointer(f_handle, 0, NULL, FILE_BEGIN);
  WriteFile( (HANDLE) f_handle, sectbuf, 512, &bytes_written, NULL ) ;

  if(bytes_written != 512) {
    fprintf(stderr, "Could not write the whole boot sector\n");
    exit(1);
  }

  /* Close file */ 
  CloseHandle(f_handle);

  /* Create the filename */
  sprintf(ldlinux_name, "%s%s", drive_name, "\\ldlinux.sys"); 

  /* Change to normal attributes to enable deletion */
  /* Just ignore error if the file do not exists */
  SetFileAttributes(ldlinux_name, FILE_ATTRIBUTE_NORMAL);

  /* Delete the file */
  /* Just ignore error if the file do not exists */
  DeleteFile(ldlinux_name);

  /* Create ldlinux.sys file */
  f_handle = CreateFile(ldlinux_name, GENERIC_READ | GENERIC_WRITE,
			FILE_SHARE_READ | FILE_SHARE_WRITE,
			NULL, CREATE_ALWAYS, 
			FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_READONLY,
			NULL );
  
  if(f_handle == INVALID_HANDLE_VALUE) {
    error("Unable to create ldlinux.sys");
    exit(1);
  }

  /* Write ldlinux.sys file */
  if (!WriteFile(f_handle, syslinux_ldlinux, syslinux_ldlinux_len, &bytes_written, NULL)) {
    error("Could not write ldlinux.sys");
    exit(1);
  }

  if (bytes_written != syslinux_ldlinux_len) {
    fprintf(stderr, "Could not write whole ldlinux.sys\n");
    exit(1);
  }

  /* Now flush the media */
  if(!FlushFileBuffers(f_handle)) {
    error("FlushFileBuffers failed");
    exit(1);
  }
    
  /* Close file */ 
  CloseHandle(f_handle);

  /* Done! */
  return 0;
}