2
* GRUB Utilities -- Utilities for GRUB Legacy, GRUB2 and GRUB for DOS
3
* Copyright (C) 2007 Bean (bean123ch@gmail.com)
5
* This program is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
26
#define BUF_SIZE 16384
28
#define OFS_HLEN 0x1F1
29
#define OFS_REDX 0x260
30
#define OFS_DLEN 0x264
32
#define APP_NAME "mkimage"
34
#define valueat(buf,ofs,type) *((type*)(((char*)&buf)+ofs))
36
void usage(int status)
40
" mkimage [OPTIONS] header [image] output\n\n"
42
" -d\t\tdrive[,part]\tSet default drive and partition\n");
46
void quit(int syserr,char *fmt, ...)
50
va_start(argptr, fmt);
52
fprintf(stderr, APP_NAME ": ");
53
vfprintf(stderr, fmt, argptr);
55
fprintf(stderr,": %s\n", strerror(errno));
64
unsigned char buf[BUF_SIZE];
66
int main(int argc, char **argv)
74
while ((ch=getopt(argc,argv, "hd:"))!= -1)
78
unsigned long drive,part;
81
drive=(strtoul(optarg,&pp,0) & 0xFF);
83
part=(strtoul(pp+1,NULL,0) & 0xFF);
86
edx=drive + (part << 16) + 0xFFFF0000;
94
if ((optind + 2 != argc) && (optind + 3 !=argc))
97
fin=fopen(argv[optind], "rb");
99
quit(1, "Unable to open %s", argv[optind]);
101
fseek(fin,0,SEEK_END);
106
quit(0, "%s too long", argv[optind]);
109
fseek(fin, 0, SEEK_SET);
110
if (fread(buf, len, 1, fin)!=1)
113
quit(0, "%s read error", argv[optind]);
119
len = (len + 511) & (~0x1FF);
120
buf[OFS_HLEN] = (len >> 9) - 1;
121
if (edx != 0xFFFFFFFF)
122
valueat(buf, OFS_REDX, unsigned long) = edx;
124
if (optind + 2 == argc)
125
pout=argv[optind + 1];
130
fin=fopen(argv[optind+1], "rb");
132
quit(1, "Unable to open %s", argv[optind+1]);
134
fseek(fin,0,SEEK_END);
140
quit(0, "%s invalid length", argv[optind+1]);
143
fseek(fin, 0, SEEK_SET);
145
valueat(buf, OFS_DLEN, unsigned long) = len1;
146
pout = argv[optind + 2];
149
fout=fopen(pout, "wb");
151
quit(1, "Unable to open %s", pout);
153
if (fwrite(buf, len, 1, fout)!=1)
156
quit(0, "%s read error", pout);
163
len = fread(buf, 1, BUF_SIZE, fin);
168
quit(1, "%s read error", argv[optind+1]);
172
if (fwrite(buf, len, 1, fout)!=1)
176
quit(1, "%s write error", pout);