summaryrefslogtreecommitdiff
path: root/packages/fis/files/fis.cc
blob: 8c43eca81160e94b5aad0bed23d1f90de99af96e (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// fis.cc
// see http://svn.chezphil.org/utils
// (C) 2007 Philip Endecott

// 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; either version 2 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


#include <string>
#include <iostream>
#include <map>

#include <boost/lexical_cast.hpp>
#include <boost/format.hpp>

#include "Exception.hh"
#include "FileDescriptor.hh"
#include "utils.hh"
#include "endian.hh"

#include <stdint.h>

using namespace std;
using namespace boost;
using namespace pbe;


static void check_dev(string device)
{
  if (device=="") {
    throw "You must specify a device using -d";
  }
}


// This is taken from drivers/mtd/redboot.c in the Linux source
struct fis_image_desc {
  char      name[16];      // Null terminated name
  uint32_t  flash_base;    // Address within FLASH of image
  uint32_t  mem_base;      // Address in memory where it executes
  uint32_t  size;          // Length of image
  uint32_t  entry_point;   // Execution entry point
  uint32_t  data_length;   // Length of actual data
  uint32_t  skips[53];
  uint32_t  desc_cksum;    // Checksum over image descriptor
  uint32_t  file_cksum;    // Checksum over image data
};

ostream& operator<<(ostream& strm, const fis_image_desc& d)
{
  strm << format("%16s: addr = 0x%08x, size = 0x%08x\n")
          % (d.name) % (d.flash_base) % (d.size);
  for (unsigned int i=0; i<(sizeof(d.skips)/4); ++i) {
    if (d.skips[i]==0x736b6970 || d.skips[i]==0x70696b73) { // "skip"
      uint32_t offset = d.skips[i+1];
      uint32_t length = d.skips[i+2];
      strm << format("                    skip: %08x + %08x\n")
              % (offset) % (length);
      i+=2;
    }
  }
  return strm;
}


void check_checksum(const fis_image_desc& d)
{
  // This isn't checked by the kernel mtd driver, which has this 
  // comment: "RedBoot doesn't actually write the desc_cksum field yet 
  // AFAICT".  I don't know what checksum is supposed to be used here.
}

void compute_checksum(fis_image_desc& d)
{
  // ditto
}


typedef map<uint32_t,fis_image_desc> dir_t;


static void swap_entry_endianness(fis_image_desc& d)
{
  d.flash_base  = swap_end_32(d.flash_base);
  d.mem_base    = swap_end_32(d.mem_base);
  d.size        = swap_end_32(d.size);
  d.entry_point = swap_end_32(d.entry_point);
  d.data_length = swap_end_32(d.data_length);
  for (unsigned int i=0; i<(sizeof(d.skips)/4); ++i) {
    d.skips[i] = swap_end_32(d.skips[i]);
  }
}


static void load_dir(FileDescriptor& fd, int offset, int size, bool swap_endianness,
                     dir_t& dir)
{
  fd.seek(offset);
  int num_entries = size/sizeof(fis_image_desc);
  for (int i=0; i<num_entries; ++i) {
    fis_image_desc d = fd.binread<fis_image_desc>();
    if (d.name[0]!=static_cast<char>(0xff)) {
      check_checksum(d);
      if (swap_endianness) {
        swap_entry_endianness(d);
      }
      dir[d.flash_base] = d;
    }
  }
}


static void write_blank_entries(FileDescriptor& fd, int n)
{
  char dummy[sizeof(fis_image_desc)];
  for (unsigned int i=0; i<sizeof(fis_image_desc); ++i) {
    dummy[i] = 0xff;
  }
  for (int i=0; i<n; ++i) {
    fd.writeall(dummy,sizeof(fis_image_desc));
  }  
}


static void save_dir(FileDescriptor& fd, int offset, int size, bool swap_endianness,
                     const dir_t& dir)
{
  fd.seek(offset);
  unsigned int num_entries = size/sizeof(fis_image_desc);
  if (num_entries<dir.size()) {
    throw "Too many entries for directory";
  }
  for (dir_t::const_iterator i=dir.begin();
       i!=dir.end(); ++i) {
    fis_image_desc d = i->second;
    compute_checksum(d);
    if (swap_endianness) {
      swap_entry_endianness(d);
    }
    fd.binwrite<fis_image_desc>(d);
  }
  write_blank_entries(fd,num_entries-dir.size());
}


static void fis_list(string device, int offset, int size, bool swap_endianness)
{
  FileDescriptor fd(device,FileDescriptor::read_only);
  dir_t dir;
  load_dir(fd,offset,size,swap_endianness,dir);
  for (dir_t::const_iterator i = dir.begin();
       i != dir.end(); ++i) {
    cout << i->second;
  }
}


static void fis_init(string device, int offset, int size)
{
  FileDescriptor fd(device,FileDescriptor::create);
  fd.seek(offset);
  int num_entries = size/sizeof(fis_image_desc);
  write_blank_entries(fd,num_entries);
}


static void check_overlap(const dir_t& dir, uint32_t addr, uint32_t size)
{
  uint32_t end_addr = addr+size;
  for (dir_t::const_iterator i = dir.begin();
       i != dir.end(); ++i) {
    if (addr<(i->second.flash_base+i->second.size)
        && end_addr>i->second.flash_base) {
      throw "New partition overlaps existing partitions";
    }
  }
}


static void fis_create(string device, int offset, int size, bool swap_endianness,
                       int argc, char* argv[])
{
  fis_image_desc d;
  d.mem_base = 0;
  d.entry_point = 0;
  d.data_length = 0;
  for (unsigned int i=0; i<(sizeof(d.skips)/4); ++i) {
    d.skips[i] = 0;
  }
  d.desc_cksum = 0;
  d.file_cksum = 0;
  
  for (int i=0; i<argc; ++i) {
    string arg=argv[i];
    if (arg=="-l") {
      if (i==argc-1) {
        throw "argumnet missing for -l";
      }
      ++i;
      d.size = maybe_hex_string_to_int(argv[i]);
    } else if (arg=="-f") {
      if (i==argc-1) {
        throw "argumnet missing for -f";
      }
      ++i;
      d.flash_base = maybe_hex_string_to_int(argv[i]);
    } else if (arg=="-n") {
      if (i==argc-1) {
        throw "argumnet missing for -n";
      }
      ++i;
      string name = argv[i];
      if (name.length()>=16) {
        throw "name too long, max 16 chars including terminating null";
      }
      for (int j=0; j<16; j++) {
        char c = name.c_str()[j];
        d.name[j] = c;
        if (!c) {
          for (; j<16; ++j) {
            d.name[j]=0;
          }
          break;
        }
      }
    } else {
      cerr << "Unrecognised option '" << arg << "'\n";
      exit(1);
    }
  }

  FileDescriptor fd(device,FileDescriptor::read_write);
  dir_t dir;
  load_dir(fd,offset,size,swap_endianness,dir);
  check_overlap(dir,d.flash_base,d.size);
  dir[d.flash_base] = d;
  save_dir(fd,offset,size,swap_endianness,dir);
}


static void fis_delete(string device, int offset, int size, bool swap_endianness,
                       string name)
{
  FileDescriptor fd(device,FileDescriptor::read_write);
  dir_t dir;
  load_dir(fd,offset,size,swap_endianness,dir);

  for (dir_t::iterator i = dir.begin();
       i != dir.end(); ++i) {
    string this_name(i->second.name);
    if (this_name == name) {
      dir.erase(i);
      save_dir(fd,offset,size,swap_endianness,dir);
      return;
    }
  }

  throw "No partition found with specified name";
}


static void usage()
{
  cerr << "Usage:\n"
       << "  fis [options] list\n"
       << "  fis [options] init\n"
       << "  fis [options] create -f address -l size -n name\n"
       << "  fis [options] delete name\n"
       << "Options:\n"
       << "  -d device    specify /dev/mtd* device containing directory\n"
       << "  -o offset    specify offset into device of start of directory\n"
       << "               (in decimal; prefix with 0x for hex)\n"
       << "  -s size      specify size of directory in bytes\n"
       << "  -e           swap endianness\n";
}


int main(int argc, char* argv[])
{
  try { try {

    if (argc==1) {
      usage();
      exit(1);
    }

    string device="";
    int offset=0;
    int size=0;
    bool swap_endianness=false;

    for (int i=1; i<argc; ++i) {
      string arg = argv[i];
      if (arg=="-d") {
        if (device!="") {
          throw "-d option used more than once";
        }
        if (i==argc-1) {
          throw "-d option is missing its parameter";
        }
        ++i;
        device = argv[i];
      } else if (arg=="-o") {
        if (offset!=0) {
          throw "-o option used more than once";
        }
        if (i==argc-1) {
          throw "-o option is missing its parameter";
        }
        ++i;
        offset = maybe_hex_string_to_int(argv[i]);
      } else if (arg=="-s") {
        if (size!=0) {
          throw "-s option used more than once";
        }
        if (i==argc-1) {
          throw "-s option is missing its parameter";
        }
        ++i;
        size = maybe_hex_string_to_int(argv[i]);
      } else if (arg=="-e") {
        swap_endianness = true;
      } else if (arg=="list") {
        if (i!=argc-1) {
          throw "Extra arguments after 'list'";
        }
        check_dev(device);
        fis_list(device,offset,size,swap_endianness);
      } else if (arg=="init") {
        if (i!=argc-1) {
          throw "Extra arguments after 'init'";
        }
        check_dev(device);
        fis_init(device,offset,size);
      } else if (arg=="create") {
        check_dev(device);
        fis_create(device,offset,size,swap_endianness,
                   argc-i-1,&argv[i+1]);
        break;
      } else if (arg=="delete") {
        if (i!=argc-2) {
          throw "Exactly one argumnet required after 'delete'";
        }
        ++i;
        string name = argv[i];
        check_dev(device);
        fis_delete(device,offset,size,swap_endianness,name);
      } else {
        cerr << "unrecognised argument '" << arg << "'\n";
        usage();
        exit(1);
      }
    }
  } RETHROW_MISC_EXCEPTIONS }
  catch (Exception& E) {
    cerr << "Error: ";
    E.report(cerr);
    exit(1);
  }
}