summaryrefslogtreecommitdiffstats
path: root/arch/ppc/treeboot/mkevimg
blob: 76f849bb708b5d9ce6959049a005167d01eb9607 (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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#!/usr/local/bin/perl

#
#    Copyright (c) 1998-1999 TiVo, Inc.
#    All rights reserved.
#
#    Copyright (c) 1999 Grant Erickson <grant@lcse.umn.edu>
#      Major syntactic and usability rework.
#
#    Module name: mkevimg
#
#    Description:
#      Converts an ELF output file from the linker into the format used by
#      the IBM evaluation board ROM Monitor to load programs from a host
#      onto the evaluation board. The ELF file must be an otherwise execut-
#      able file (with the text and data addresses bound at link time) and
#      have space reserved after the entry point for the load information
#      block:
#
#      typedef struct boot_block {
#        unsigned long magic;	        0x0052504F
#	 unsigned long dest;	        Target address of the image
#	 unsigned long num_512blocks;   Size, rounded-up, in 512 byte blocks
#	 unsigned long debug_flag;      Run the debugger or image after load
#        unsigned long entry_point;	The image address to jump to after load
#	 unsigned long reserved[3];
#      } boot_block_t;
#
#   

use File::Basename;
use Getopt::Std;

#
# usage()
#
# Description:
#   This routine prints out the proper command line usage for this program
#
# Input(s):
#   status - Flag determining what usage information will be printed and what
#            the exit status of the program will be after the information is
#            printed.
#
# Output(s):
#   N/A
#
# Returns:
#   This subroutine does not return.
#

sub usage {
  my($status);
  $status = $_[0];

  printf("Usage: %s [-hlvV] <ELF input file> <Evaluation board output file>\n",
	$program);

  if ($status != 0) {
    printf("Try `%s -h' for more information.\n", $program);
  }

  if ($status != 1) {
    print("  -h         Print out this message and exit.\n");
    print("  -l         Linux mode; if present, copy 'image' and 'initrd' sections.\n");
    print("  -v         Verbose. Print out lots of ELF information.\n");
    print("  -V         Print out version information and exit.\n");
  }

  exit($status);
}

#
# version()
#
# Description:
#   This routine prints out program version information
#
# Input(s):
#   N/A
#
# Output(s):
#   N/A
#
# Returns:
#   This subroutine does not return.
#

sub version {
  print("mkevimg Version 1.1.0\n");
  print("Copyright (c) 1998-1999 TiVo, Inc.\n");
  print("Copyright (c) 1999 Grant Erickson <grant\@lcse.umn.edu>\n");

  exit (0);
}

#
# file_check()
#
# Description:
#   This routine checks an input file to ensure that it exists, is a
#   regular file, and is readable.
#
# Input(s):
#   file - Input file to be checked.
#
# Output(s):
#   N/A
#
# Returns:
#   0 if the file exists, is a regular file, and is readable, otherwise -1.
#

sub file_check {
  my($file);
  $file = $_[0];

  if (!(-e $file)) {
    printf("The file \"%s\" does not exist.\n", $file);
    return (-1);
  } elsif (!(-f $file)) {
    printf("The file \"%s\" is not a regular file.\n", $file);
    return (-1);
  } elsif (!(-r $file)) {
    printf("The file \"%s\" is not readable.\n", $file);
    return (-1);
  }

  return (0);
}

#
# decode_options()
#
# Description:
#   This routine steps through the command-line arguments, parsing out
#   recognzied options.
#
# Input(s):
#   N/A
#
# Output(s):
#   N/A
#
# Returns:
#   N/A
#

sub decode_options {

  if (!getopts("hlvV")) {
    usage(1);
  }

  if ($opt_h) {
    usage(0);
  }

  if ($opt_l) {
    $linux = 1;
  }

  if ($opt_V) {
    version();
    exit (0);
  }

  if ($opt_v) {
    $verbose = 1;
  }

  if (!($ifile = shift(@ARGV))) {
    usage(1);
  }

  if (!($ofile = shift(@ARGV))) {
    usage (1);
  }

  if (file_check($ifile)) {
    exit(1);
  }

}

#
# ELF file and section header field numbers
#

require 'elf.pl';

#
# Main program body
#

{
  $program = basename($0);

  decode_options();

  open(ELF, "<$ifile") || die "Cannot open input file";

  $ifilesize = (-s $ifile);

  if ($verbose) {
    print("Output file: $ofile\n");
    print("Input file: $ifile, $ifilesize bytes.\n");
  }

  if (read(ELF, $ibuf, $ifilesize) != $ifilesize) {
    print("Failed to read input file!\n");
    exit(1);
  }

  # 
  # Parse ELF header
  #

  @eh = unpack("a16n2N5n6", $ibuf);

  #
  # Make sure this is actually a PowerPC ELF file.
  #

  if (substr($eh[$e_ident], 0, 4) ne "\177ELF") {
    printf("The file \"%s\" is not an ELF file.\n", $ifile);
    exit (1);
  } elsif ($eh[$e_machine] != 20) {
    printf("The file \"%s\" is not a PowerPC ELF file.\n", $ifile);
    exit (1);
  }

  if ($verbose) {
    print("File header:\n");
    printf("  Identifier:            %s\n",	$eh[$e_ident]);
    printf("  Type:                  %d\n",	$eh[$e_type]);
    printf("  Machine:               %d\n",	$eh[$e_machine]);
    printf("  Version:               %d\n",	$eh[$e_version]);
    printf("  Entry point:           0x%08x\n", $eh[$e_entry]);
    printf("  Program header offset: 0x%x\n",	$eh[$e_phoff]);
    printf("  Section header offset: 0x%x\n",	$eh[$e_shoff]);
    printf("  Flags:                 0x%08x\n", $eh[$e_flags]);
    printf("  Header size:           %d\n", $eh[$e_ehsize]);
    printf("  Program entry size:    %d\n", $eh[$e_phentsize]);
    printf("  Program table entries: %d\n", $eh[$e_phnum]);
    printf("  Section header size:   %d\n", $eh[$e_shentsize]);
    printf("  Section table entries: %d\n", $eh[$e_shnum]);
    printf("  String table section:  %d\n", $eh[$e_shstrndx]);
  }

  #
  # Find the section header for the string table.
  #

  $strtable_section_offset =  $eh[$e_shoff] +
    $eh[$e_shstrndx] * $eh[$e_shentsize];

  if ($verbose) {
    printf("String table section header offset: 0x%x\n",
	   $strtable_section_offset);
  }
  
  #
  # Find the start of the string table.
  #

  @strh = unpack("N10", substr($ibuf, $strtable_section_offset, 
			       $eh[$e_shentsize]));

  if ($verbose) {
    printf("Section name strings start at: 0x%x, %d bytes.\n",
	   $strh[$sh_offset], $strh[$sh_size]);
  }

  $names = substr($ibuf, $strh[$sh_offset], $strh[$sh_size]);

  # Grab each section header and find '.text' and '.bss' sections in
  # particular.

  if ($verbose) {
  print("Section headers:\n");
  print("Idx  Name                      Size      Address   File off  Algn\n");
  print("---  ------------------------  --------  --------  --------  ----\n");
  }

  $off = $eh[$e_shoff];

  for($i = 0; $i < $eh[$e_shnum]; $i++, $off += $eh[$e_shentsize]) {
    @sh = unpack("N10", substr($ibuf, $off, $eh[$e_shentsize]));

    # Take the first section name from the array returned by split.

    ($name) = split(/\000/, substr($names, $sh[$sh_name]));
    
    if ($verbose) {
      printf("%3d  %-24s  %8x  %08x  %08x  %4d\n", 
	     $i, $name, $sh[$sh_size], $sh[$sh_addr], 
	     $sh[$sh_offset], $sh[$sh_addralign]);
    }

    # Attempt to find the .text and .bss sections

    if ($name =~ /^\.bss$/) {
      ($bss_addr, $bss_offset, $bss_size) =
	($sh[$sh_addr], $sh[$sh_offset], $sh[$sh_size]);

    } elsif ($name =~ /^\.text$/) {
      ($text_addr, $text_offset, $text_size) = 
	($sh[$sh_addr], $sh[$sh_offset], $sh[$sh_size]);

    } elsif ($linux && ($name =~ /^\image$/)) {
      $image_found = 1;

      ($image_addr, $image_offset, $image_size) = 
	($sh[$sh_addr], $sh[$sh_offset], $sh[$sh_size]);

    } elsif ($linux && ($name =~ /^\initrd$/)) {
      $initrd_found = 1;

      ($initrd_addr, $initrd_offset, $initrd_size) = 
	($sh[$sh_addr], $sh[$sh_offset], $sh[$sh_size]);

    }
  }

  printf("Text section   - Address: 0x%08x, Size: 0x%08x\n",
	 $text_addr, $text_size);
  printf("Bss  section   - Address: 0x%08x, Size: 0x%08x\n",
	 $bss_addr, $bss_size);

  if ($linux) {
    if ($image_found) {
      printf("Image section  - Address: 0x%08x, Size: 0x%08x\n",
	     $image_addr, $image_size);
    }

    if ($initrd_found) {
      printf("Initrd section - Address: 0x%08x, Size: 0x%08x\n",
	     $initrd_addr, $initrd_size);
    }
  }

  #
  # Open output file
  #

  open(BOOT, ">$ofile") || die "Cannot open output file";

  #
  # Compute image size
  #

  $output_size = $bss_offset - $text_offset + $bss_size;

  if ($linux && $image_found) {
    $output_size += $image_size;
  }

  if ($linux && $initrd_found) {
    $output_size += $initrd_size;
  }

  $num_blocks = $output_size / 512 + 1;

  #
  # Write IBM PowerPC evaluation board boot_block_t header
  #

  $header = pack("H8N7", "0052504f", $text_addr, $num_blocks, 0,
		 $text_addr, 0, 0, 0);

  $bytes = length($header);
  
  if (($resid = syswrite(BOOT, $header, $bytes)) != $bytes) {
    die("Could not write boot image header to output file.");
  }

  printf("Entry point = 0x%08x\n", $text_addr);
  printf("Image size  = 0x%08x (%d bytes) (%d blocks).\n", 
	 $output_size, $output_size, $num_blocks);

  #
  # Write image starting after ELF and program headers and 
  # continuing to beginning of bss
  #

  $bytes = $bss_offset - $text_offset + $bss_size;

  if (($resid = syswrite(BOOT, $ibuf, $bytes, $text_offset)) != $bytes) {
    die("Could not write boot image to output file.\n");
  }

  #
  # If configured, write out the image and initrd sections as well
  #

  if ($linux) {
    if ($image_found) {
      $bytes = $image_size;
      if (($resid = syswrite(BOOT, $ibuf, $bytes, $image_offset)) != $bytes) {
	die("Could not write boot image to output file.\n");
      }
    }

    if ($initrd_found) {
      $bytes = $initrd_size;
      if (($resid = syswrite(BOOT, $ibuf, $bytes, $initrd_offset)) != $bytes) {
	die("Could not write boot image to output file.\n");
      }
    }
  }

  #
  # Pad to a multiple of 512 bytes
  #

  $pad_size = 512 - (length($header) + $output_size) % 512;

  if ($verbose) {
    print("Padding boot image by an additional $pad_size bytes.\n");
  }

  $pad_string = pack(("H8","deadbeef") x 128);

  syswrite(BOOT, $pad_string, $pad_size) or
    die "Could not pad boot image in output file.\n";

  #
  # Clean-up and leave
  #

  close(BOOT);

  print("\nBoot image file \"$ofile\" built successfuly.\n\n");

  exit(0);
}