3 #*****************************************************************************
5 #! FILE NAME : boot_linux
7 #! PARAMETERS : -b <bootimage> the name of the boot image to use
8 #! -d <device> the interface to use, e.g., eth1
10 #! -f save it in flash memory at address 0x10000
11 #! -F save it in flash memory at address 0
13 #! -i <image> name of the image to use (default is fimage)
14 #! -o <offset> the offset in the flash where the flashing
16 #! -O <offset> the offset in the image file where the
17 #! flashing starts from
18 #! -p print the resulting etrax100boot command
19 #! instead of executing it
20 #! -s <size> how much to flash (default is the size of
21 #! the flash minus the offset specified using
23 #! -S <size> the size of the flash
25 #! All sizes and offsets above can be specified as decimal
26 #! numbers, or as hexadecimal numbers by prefixing them with 0x.
27 #! It is also possible to use the suffixes k and M to specify
28 #! kilo (1024) or mega (1048576).
30 #! DESCRIPTION: Extract the start of the image and any registers that should
31 #! be set from the kimage or fimage file, and then boot it.
33 #! FUNCTIONS : convert_size
34 #! extract_hw_settings
36 #! calculate_sdram_init
40 #!----------------------------------------------------------------------------
43 #! $Log: boot_linux,v $
44 #! Revision 1.16 2004/11/01 16:32:27 starvik
45 #! Corrected help text to avoid confusion
47 #! Revision 1.15 2003/01/29 11:48:57 pkj
48 #! Calculate a flash size large enough for the given image if the
49 #! -S option is not specified.
51 #! Revision 1.14 2002/11/18 14:40:09 pkj
52 #! Make use of the --loop option to etrax100boot when initialising
53 #! SDRAM memories. This requires a lot fewer options to be passed
54 #! to the boot loader.
56 #! Revision 1.13 2002/08/15 16:29:02 pkj
57 #! * The -S option now accepts the size in bytes (just like the -s option).
58 #! For backwards compatibility it still assumes sizes of 16 and less to
59 #! be specified in MB.
60 #! * The suffixes k and M can now be used with all sizes and offsets to
61 #! specify them in kilo or mega.
63 #! Revision 1.12 2002/08/15 15:27:34 pkj
64 #! Use $opts{'x'} instead of $opt_x.
66 #! Revision 1.11 2002/07/04 17:06:39 pkj
67 #! * No longer specifies a bootfile by default (not needed any longer).
68 #! * Implemented option -b to specify a bootfile.
69 #! * Removed references to option -l (it was never implemented).
71 #! Revision 1.10 2002/06/04 11:50:23 starvik
72 #! Check if mrs_data is specified in kernelconfig (necessary for MCM)
74 #! Revision 1.9 2002/01/29 10:38:26 pkj
75 #! Change illegal to invalid.
77 #! Revision 1.8 2001/09/13 12:32:10 pkj
78 #! * Added option -S to specify the size of the flash (in MB), as -s
79 #! is used to specify how much to flash nowadays.
80 #! * Made the default size of the flash depend on the size of the image
81 #! file. If it is bigger than 0x200100 then the flash is assumed to
82 #! be 4 MB, otherwise it is assumed to be 2 MB.
83 #! * Added verification of various options.
85 #! Revision 1.7 2001/09/13 10:25:11 pkj
88 #! Revision 1.6 2001/06/29 10:05:16 pkj
89 #! Corrected check for SDRAM.
91 #! Revision 1.5 2001/06/29 09:11:55 pkj
92 #! Synchronised boot_elinux and boot_linux.
94 #!----------------------------------------------------------------------------
95 #! (C) Copyright 2001, Axis Communications AB, LUND, SWEDEN
96 #!****************************************************************************
97 # $Id: boot_linux,v 1.16 2004/11/01 16:32:27 starvik Exp $
99 #****************** INCLUDE FILES SECTION ************************************
106 #****************** VARIABLE DECLARATION SECTION *****************************
108 use vars
qw($my_name %opts);
109 use vars qw($text_start $cmd);
110 use vars qw($image_name $image_size);
111 use vars qw($offset $source_offset $flash_size $flashing_size);
112 use vars qw($sdram_timing_address $sdram_config_address);
113 use vars qw($sdram_precharge $sdram_nop $sdram_refresh $sdram_mrs);
115 #****************** CONSTANT SECTION *****************************************
118 $sdram_timing_address = "b0000008";
119 $sdram_config_address = "b000000c";
122 $sdram_precharge = 3;
127 #****************** MAIN PROGRAM SECTION *************************************
129 # The name of this program.
130 $my_name = basename($0);
133 getopts('b:d:fFhi:o:O:ps:S:', \%opts);
135 &print_help if ($opts{'h'});
137 # Name and existance of the image
138 $image_name = ($opts{'i'} ? $opts{'i'} : 'fimage');
139 die "Could not find the image $image_name!\n" unless (-s $image_name);
141 if ($opts{'f'} || $opts{'F'})
143 $image_size = -s $image_name;
145 $offset = ($opts{'f'} ? 0x10000 : 0);
147 $offset = &convert_size($opts{'o'}) if (defined($opts{'o'}));
149 die("$my_name: Invalid destination offset\n") if ($offset !~ /^\d+$/);
151 my $base_name = basename($image_name);
152 if ($base_name eq 'timage' || $base_name eq 'flash1.img')
158 $source_offset = $offset;
161 $source_offset = &convert_size($opts{'O'}) if (defined($opts{'O'}));
163 die("$my_name: Invalid source offset\n") if ($source_offset !~ /^\d+$/);
164 die("$my_name: Source offset > image size\n") if ($source_offset > $image_size);
166 if (defined($opts{'S'}))
168 # Backwards compatibility to allow specifying the flash size in MB
169 # without using an M suffix
170 $opts{'S'} .= 'M' if ($opts{'S'} =~ /^\d+$/ && $opts{'S'} <= 16);
172 $flash_size = &convert_size($opts{'S'});
176 # Calculate a flash size large enough for the image without the checksum
178 $flash_size = ($image_size - $source_offset + $offset) & 0xFFFF0000;
181 die("$my_name: Invalid flash size\n") if ($flash_size !~ /^\d+$/);
182 die("$my_name: Destination offset > flash size\n") if ($offset > $flash_size);
183 if (defined($opts{'s'}))
185 $flashing_size = &convert_size($opts{'s'});
189 $flashing_size = $flash_size - $offset;
192 die("$my_name: Invalid size to flash\n") if ($flashing_size !~ /^\d+$/);
194 if ($flashing_size > $flash_size - $offset)
196 $flashing_size = $flash_size - $offset;
197 printf("Warning: Flashing size limited to 0x%lx due to the offset (0x%lx) and flash size (0x%lx).\n", $flashing_size, $offset, $flash_size);
200 if ($flashing_size > $image_size - $source_offset)
202 $flashing_size = $image_size - $source_offset;
203 printf("Warning: Flashing size limited to 0x%lx due to the offset (0x%lx) and image size (0x%lx).\n", $flashing_size, $source_offset, $image_size);
207 # Create the command line to boot the image
208 if (system('./etrax100boot --help > /dev/null') == 0)
210 $cmd = './etrax100boot';
212 elsif (system('svinto_boot --help > /dev/null') == 0)
214 $cmd = 'svinto_boot';
218 die("Cannot find e100boot program in your PATH!\n");
221 $cmd .= " --device $opts{'d'}" if ($opts{'d'});
223 $cmd .= &extract_hw_settings;
225 $cmd .= " --bootfile $opts{'b'}" if ($opts{'b'});
226 $cmd .= " --file $image_name $text_start";
228 if ($opts{'f'} || $opts{'F'})
230 $cmd .= sprintf(" --flash %lx %lx %lx --jump 0",
231 hex($text_start) + $source_offset, $offset, $flashing_size);
235 $cmd .= " --jump $text_start";
240 print "Command:\n$cmd\n";
249 #****************** FUNCTION DEFINITION SECTION ******************************
251 #*****************************************************************************
253 ## FUNCTION NAME: convert_size
255 ##****************************************************************************
262 if ($arg =~ /^0x([\da-fA-F]+)([kM])?$/)
266 elsif ($arg =~ /^(\d+)([kM])?$/)
285 return $size * 1048576;
289 #*****************************************************************************
291 ## FUNCTION NAME: extract_hw_settings
293 ##****************************************************************************
295 sub extract_hw_settings
300 my $return_value = "";
303 # The hw information table has the following format
307 # serial debg port (dword)
308 # sdram enabled (dword)
309 # register address (dword)
310 # register value (dword)
314 open(FILE, "$image_name") || die("Could not open '$image_name'");
318 if (m/HW_PARAM_MAGIC/g)
320 # Seek to first byte after magic
321 seek(FILE, -length($_) + pos($_), 1);
326 $text_start = &get_dword;
327 $dbg_port = &get_dword;
328 $sdram_enabled = int(&get_dword);
332 my $register = &get_dword;
333 my $value = &get_dword;
335 last if ($register eq "00000000");
339 if ($register eq $sdram_config_address)
341 $sdram_config = $value;
343 elsif ($register eq $sdram_timing_address)
345 $return_value .= &calculate_sdram_init($value, $sdram_config);
350 $return_value .= " --setreg $register $value";
355 return $return_value;
358 #*****************************************************************************
360 ## FUNCTION NAME: get_dword
362 ##****************************************************************************
368 read(FILE, $data, 4);
369 return unpack("H8", pack("V", unpack("N", $data)));
372 #*****************************************************************************
374 ## FUNCTION NAME: calculate_sdram_init
376 ##****************************************************************************
378 sub calculate_sdram_init
380 # Refer to ETRAX 100LX Designers Reference for a description of SDRAM
382 my $sdram_init_val = hex($_[0]);
383 my $sdram_config_val = hex($_[1]);
384 my $bus_width = $sdram_config_val & 0x00800000;
392 $mrs_data = ($sdram_init_val & 0x00ff0000) >> 16;
393 $sdram_init_val &= 0x8000ffff; # Make sure mrs data is 0
394 $sdram_init_val |= 0x80000000; # Make sure sdram is enabled
395 $speed = $sdram_init_val & 0x1000;
396 $cas_latency = $sdram_init_val & 0x3;
397 if ($speed) # 100 MHz
406 # Calculate value of mrs_data
407 # CAS latency = 2 && bus_width = 32 => 0x40
408 # CAS latency = 3 && bus_width = 32 => 0x60
409 # CAS latency = 2 && bus_width = 16 => 0x20
410 # CAS latency = 3 && bus_width = 16 => 0x30
413 if ($bus_width == 0) # 16 bits
415 $mrs_data = $cas_latency == 2 ? 0x20 : 0x30;
419 $mrs_data = $cas_latency == 2 ? 0x40 : 0x60;
423 $temp = $sdram_init_val | 0x0000c000; # Disable refresh
424 $return_value .= &sdram_command($temp);
425 $return_value .= " --pause 20000";
427 $return_value .= &sdram_command($temp, $sdram_precharge);
428 $return_value .= &sdram_command($temp, $sdram_nop);
430 $return_value .= " --setreg +0 7";
431 $return_value .= " --label label1";
432 $return_value .= &sdram_command($temp, $sdram_refresh);
433 $return_value .= &sdram_command($temp, $sdram_nop);
434 $return_value .= " --loop +0 label1";
436 $return_value .= &sdram_command($temp, $sdram_mrs, $mrs_data);
437 $return_value .= &sdram_command($temp, $sdram_nop);
439 $return_value .= &sdram_command($sdram_init_val);
441 return $return_value;
444 #*****************************************************************************
446 ## FUNCTION NAME: sdram_command
448 ##****************************************************************************
452 my($temp, $value, $mrs_data) = @_;
455 if ($value == $sdram_mrs)
457 $value = sprintf("%lx", $temp | ($value << 9) | ($mrs_data << 16));
461 $value = sprintf("%lx", $temp | ($value << 9));
464 return " --setreg $sdram_timing_address $value";
467 #*****************************************************************************
469 ## FUNCTION NAME: print_help
471 ##****************************************************************************
475 print "\nAXIS $my_name, ", '$Revision: 1.16 $ $Date: 2004/11/01 16:32:27 $ ', "\n";
477 Copyright (C) 2001-2002 Axis Communications AB
480 This program is used to boot (and flash) a linux image to a box.
481 It tries to extract the required ETRAX 100 settings from the image file.
487 -b <bootfile> : The boot image to use.
488 -d <device> : The network interface to use, default is eth0.
489 -f : Save the image in the flash memory starting at
491 -F : Save the image in the flash memory starting at
493 -h : Print this help text.
494 -i <image> : The path and name of the image to use, default
496 -o <offset> : The offset in the flash where the flashing starts.
497 -O <offset> : The offset in the image file where the flashing
499 -p : Print the resulting etrax100boot command instead
501 -s <size> : How much to flash (default is the size of the
502 flash minus the offset specified using -o or -f).
503 -S <size> : The size of the flash.
505 All sizes and offsets above can be specified as decimal numbers, or as
506 hexadecimal numbers by prefixing them with 0x. It is also possible to use
507 the suffixes k and M to specify kilo (1024) or mega (1048576).
512 #****************** END OF FILE boot_linux ***********************************
This page took 0.095873 seconds and 5 git commands to generate.