summaryrefslogtreecommitdiffstats
path: root/arch/ppc/treeboot/mkirimg
blob: e8aa24e3d1c7b1e0a564f06312b2c4a25461098f (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
#!/usr/local/bin/perl
#
#    Copyright (c) 1998-1999 TiVo, Inc.
#      Original ELF parsing code.
#
#    Copyright (c) 1999 Grant Erickson <grant@lcse.umn.edu>
#      Original code from 'mkevimg'.
#
#    Module name: mkirimg
#
#    Description:
#      Reads an ELF file and assigns global variables 'imageSect_start',
#      'imageSect_size', 'initrdSect_start', and 'initrdSect_size' from
#      the "image" and "initrd" section header information. It then
#      rewrites the input ELF file with assigned globals to an output
#      file.
#
#      An input file, "irSectStart.txt" has the memory address of
#      'irSectStart'. The irSectStart memory address is used to find
#      the global variables in the ".data" section of the ELF file.
#      The 'irSectStart' and the above global variables are defined
#      in "irSect.c".
#
#

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 [-hvV] <ELF input file> <Evaluation board output file> <irSectStart.txt 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("  -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("mkirimg 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("hvV")) {
      usage(1);
  }

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

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

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

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

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

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

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

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

#
# ELF file and section header field numbers
#

require 'elf.pl';

#
# Main program body
#

{
  $program = basename($0);
  decode_options();

  open(ELF, "<$ElfFile") || die "Cannot open input file";
  open(OUTPUT, ">$OutputFile") || die "Cannot open output file";
  open(IR, "$IrFile") || die "Cannot open input file";

  $ElfFilesize = (-s $ElfFile);

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

  if (read(IR, $irbuf, 8) != 8) {
      print("Failed to read Ir 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", $ElfFile);
      exit (1);
  } elsif ($eh[$e_machine] != 20) {
      printf("The file \"%s\" is not a PowerPC ELF file.\n", $ElfFile);
      exit (1);
  }

  #
  # 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 '.data', 'image', and 
  # 'initrd' sections in particular.

  $off = $eh[$e_shoff];
  $imageFound = 0;
  $initrdFound = 0;
  
  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]));

    # Attempt to find the .data, image, and initrd sections

    if ($name =~ /^\image$/) {
      ($image_addr, $image_offset, $image_size) =
	($sh[$sh_addr], $sh[$sh_offset], $sh[$sh_size]);
      $imageFound = 1;

    } elsif ($name =~ /^\initrd$/) {
      ($initrd_addr, $initrd_offset, $initrd_size) =
	($sh[$sh_addr], $sh[$sh_offset], $sh[$sh_size]);
      $initrdFound = 1;

    } elsif ($name =~ /^\.data$/) {
      ($data_addr, $data_offset, $data_size) =
	($sh[$sh_addr], $sh[$sh_offset], $sh[$sh_size]);

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

    }
   }

  if ($verbose) {
    printf("Data section   - Address: 0x%08x, Size: 0x%08x, File Offset 0x%08x\n",
	   $data_addr, $data_size, $data_offset);
    printf("Bss  section   - Address: 0x%08x, Size: 0x%08x, File Offset 0x%08x\n",
	 $bss_addr, $bss_size, $bss_offset);
   }

  if ($verbose) {
    if ($imageFound) {
      printf("Image section  - Address: 0x%08x, Size: 0x%08x\n",
	     $image_addr, $image_size);
    } else {
      printf("Image section not found in file: $ElfFile\n");
    }

    if ($initrdFound) {
      printf("Initrd section - Address: 0x%08x, Size: 0x%08x\n",
	     $initrd_addr, $initrd_size);
    } else {
      printf("Initrd section not found in file: $ElfFile\n");
    }
  }

  # get file offset of irSectStart

  $irSectStartoffset = hex ($irbuf);

  if ($verbose) {
    printf("irSectStartOffset Address: 0x%08x\n", $irSectStartoffset);
  }

  # get the offset of global variables 

  $initialOffset = ($irSectStartoffset - $data_addr) + $data_offset + 4;

  # write modified values to OUTPUT file

  syswrite(OUTPUT, $ibuf, $initialOffset);

  if ($imageFound) {
    $testN = pack ("I2", $bss_addr + $bss_size, $image_size);
    syswrite(OUTPUT, $testN, length($testN));
    printf("Updated symbol \"imageSect_start\" to 0x%08x\n",
	   $bss_addr + $bss_size);
    printf("Updated symbol \"imageSect_size\" to 0x%08x\n", $image_size);
  } else {
    syswrite(OUTPUT, $ibuf, 8, $initialOffset);
  }

  if ($initrdFound) {
    $testN = pack ("I2", $bss_addr + $bss_size + $image_size, $initrd_size);
    syswrite(OUTPUT, $testN, length($testN));
    printf("Updated symbol \"initrdSect_start\" to 0x%08x\n",
	   $bss_addr + $bss_size + $image_size);
    printf("Updated symbol \"initrdSect_size\" to 0x%08x\n", $initrd_size);
  } else {
    syswrite(OUTPUT, $ibuf,8, $initialOffset + 8);
  }

  syswrite(OUTPUT, $ibuf, $ElfFilesize - ($initialOffset + 16),
	   $initialOffset + 16);

  #
  # Clean-up and leave
  #

  close (ELF);
  close (OUTPUT);
  close (IR);

  exit (0);
}