~smartboyhw/wubi/bug-1080090-new

« back to all changes in this revision

Viewing changes to src/grubutil/loadbin/.svn/text-base/mkimage.c.svn-base

  • Committer: Howard Chan
  • Date: 2012-11-20 10:16:05 UTC
  • Revision ID: smartboyhw@gmail.com-20121120101605-qfmjfsdynpzg9an9
Added images

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  GRUB Utilities --  Utilities for GRUB Legacy, GRUB2 and GRUB for DOS
3
 
 *  Copyright (C) 2007 Bean (bean123ch@gmail.com)
4
 
 *
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.
9
 
 *
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.
14
 
 *
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/>.
17
 
 */
18
 
 
19
 
#include <stdio.h>
20
 
#include <stdlib.h>
21
 
#include <stdarg.h>
22
 
#include <string.h>
23
 
#include <getopt.h>
24
 
#include <errno.h>
25
 
 
26
 
#define BUF_SIZE        16384
27
 
 
28
 
#define OFS_HLEN        0x1F1
29
 
#define OFS_REDX        0x260
30
 
#define OFS_DLEN        0x264
31
 
 
32
 
#define APP_NAME        "mkimage"
33
 
 
34
 
#define valueat(buf,ofs,type)   *((type*)(((char*)&buf)+ofs))
35
 
 
36
 
void usage(int status)
37
 
{
38
 
  fprintf(stderr,
39
 
          "Usage:\n"
40
 
          "  mkimage [OPTIONS] header [image] output\n\n"
41
 
          "Options:\n"
42
 
          "  -d\t\tdrive[,part]\tSet default drive and partition\n");
43
 
  exit(status);
44
 
}
45
 
 
46
 
void quit(int syserr,char *fmt, ...)
47
 
{
48
 
  va_list argptr;
49
 
 
50
 
  va_start(argptr, fmt);
51
 
 
52
 
  fprintf(stderr, APP_NAME ": ");
53
 
  vfprintf(stderr, fmt, argptr);
54
 
  if (syserr)
55
 
    fprintf(stderr,": %s\n", strerror(errno));
56
 
  else
57
 
    fprintf(stderr,"\n");
58
 
 
59
 
  va_end(argptr);
60
 
 
61
 
  exit(1);
62
 
}
63
 
 
64
 
unsigned char buf[BUF_SIZE];
65
 
 
66
 
int main(int argc, char **argv)
67
 
{
68
 
  char ch,*pout;
69
 
  FILE *fin,*fout;
70
 
  long ofs,len,i;
71
 
  unsigned long edx;
72
 
 
73
 
  edx=0xFFFFFFFF;
74
 
  while ((ch=getopt(argc,argv, "hd:"))!= -1)
75
 
    switch (ch) {
76
 
    case 'd':
77
 
      {
78
 
        unsigned long drive,part;
79
 
        char *pp;
80
 
 
81
 
        drive=(strtoul(optarg,&pp,0) & 0xFF);
82
 
        if (*pp==',')
83
 
          part=(strtoul(pp+1,NULL,0) & 0xFF);
84
 
        else
85
 
          part=0xFF;
86
 
        edx=drive + (part << 16) + 0xFFFF0000;
87
 
        break;
88
 
      }
89
 
    case 'h':
90
 
    default:
91
 
      usage(0);
92
 
    }
93
 
 
94
 
  if ((optind + 2 != argc) && (optind + 3 !=argc))
95
 
    usage(1);
96
 
 
97
 
  fin=fopen(argv[optind], "rb");
98
 
  if (fin == NULL)
99
 
    quit(1, "Unable to open %s", argv[optind]);
100
 
 
101
 
  fseek(fin,0,SEEK_END);
102
 
  len = ftell(fin);
103
 
  if (len >= BUF_SIZE)
104
 
    {
105
 
      fclose(fin);
106
 
      quit(0, "%s too long", argv[optind]);
107
 
    }
108
 
 
109
 
  fseek(fin, 0, SEEK_SET);
110
 
  if (fread(buf, len, 1, fin)!=1)
111
 
    {
112
 
      fclose(fin);
113
 
      quit(0, "%s read error", argv[optind]);
114
 
    }
115
 
 
116
 
  fclose(fin);
117
 
  fin = NULL;
118
 
 
119
 
  len = (len + 511) & (~0x1FF);
120
 
  buf[OFS_HLEN] = (len >> 9) - 1;
121
 
  if (edx != 0xFFFFFFFF)
122
 
    valueat(buf, OFS_REDX, unsigned long) = edx;
123
 
 
124
 
  if (optind + 2 == argc)
125
 
    pout=argv[optind + 1];
126
 
  else
127
 
    {
128
 
      long len1;
129
 
 
130
 
      fin=fopen(argv[optind+1], "rb");
131
 
      if (fin == NULL)
132
 
        quit(1, "Unable to open %s", argv[optind+1]);
133
 
 
134
 
      fseek(fin,0,SEEK_END);
135
 
      len1 = ftell(fin);
136
 
 
137
 
      if (len1 <= 0)
138
 
        {
139
 
          fclose(fin);
140
 
          quit(0, "%s invalid length", argv[optind+1]);
141
 
        }
142
 
 
143
 
      fseek(fin, 0, SEEK_SET);
144
 
 
145
 
      valueat(buf, OFS_DLEN, unsigned long) = len1;
146
 
      pout = argv[optind + 2];
147
 
    }
148
 
 
149
 
  fout=fopen(pout, "wb");
150
 
  if (fout == NULL)
151
 
    quit(1, "Unable to open %s", pout);
152
 
 
153
 
  if (fwrite(buf, len, 1, fout)!=1)
154
 
    {
155
 
      fclose(fout);
156
 
      quit(0, "%s read error", pout);
157
 
    }
158
 
 
159
 
  if (fin)
160
 
    {
161
 
      while (1)
162
 
        {
163
 
          len = fread(buf, 1, BUF_SIZE, fin);
164
 
          if (len < 0)
165
 
            {
166
 
              fclose(fin);
167
 
              fclose(fout);
168
 
              quit(1, "%s read error", argv[optind+1]);
169
 
            }
170
 
          if (len == 0)
171
 
            break;
172
 
          if (fwrite(buf, len, 1, fout)!=1)
173
 
            {
174
 
              fclose(fin);
175
 
              fclose(fout);
176
 
              quit(1, "%s write error", pout);
177
 
            }
178
 
        }
179
 
      fclose(fin);
180
 
    }
181
 
  fclose(fout);
182
 
 
183
 
  return 0;
184
 
}