boot_linux 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. #!/usr/bin/perl -w
  2. #*****************************************************************************
  3. #!
  4. #! FILE NAME : boot_linux
  5. #!
  6. #! PARAMETERS : -b <bootimage> the name of the boot image to use
  7. #! -d <device> the interface to use, e.g., eth1
  8. #! (defaults is eth0)
  9. #! -f save it in flash memory at address 0x10000
  10. #! -F save it in flash memory at address 0
  11. #! -h show some help
  12. #! -i <image> name of the image to use (default is fimage)
  13. #! -o <offset> the offset in the flash where the flashing
  14. #! starts
  15. #! -O <offset> the offset in the image file where the
  16. #! flashing starts from
  17. #! -p print the resulting etrax100boot command
  18. #! instead of executing it
  19. #! -s <size> how much to flash (default is the size of
  20. #! the flash minus the offset specified using
  21. #! -o or -f)
  22. #! -S <size> the size of the flash
  23. #!
  24. #! All sizes and offsets above can be specified as decimal
  25. #! numbers, or as hexadecimal numbers by prefixing them with 0x.
  26. #! It is also possible to use the suffixes k and M to specify
  27. #! kilo (1024) or mega (1048576).
  28. #!
  29. #! DESCRIPTION: Extract the start of the image and any registers that should
  30. #! be set from the kimage or fimage file, and then boot it.
  31. #!
  32. #! FUNCTIONS : convert_size
  33. #! extract_hw_settings
  34. #! get_dword
  35. #! calculate_sdram_init
  36. #! sdram_command
  37. #! print_help
  38. #!
  39. #!----------------------------------------------------------------------------
  40. #! HISTORY
  41. #!
  42. #! $Log: boot_linux,v $
  43. #! Revision 1.16 2004/11/01 16:32:27 starvik
  44. #! Corrected help text to avoid confusion
  45. #!
  46. #! Revision 1.15 2003/01/29 11:48:57 pkj
  47. #! Calculate a flash size large enough for the given image if the
  48. #! -S option is not specified.
  49. #!
  50. #! Revision 1.14 2002/11/18 14:40:09 pkj
  51. #! Make use of the --loop option to etrax100boot when initialising
  52. #! SDRAM memories. This requires a lot fewer options to be passed
  53. #! to the boot loader.
  54. #!
  55. #! Revision 1.13 2002/08/15 16:29:02 pkj
  56. #! * The -S option now accepts the size in bytes (just like the -s option).
  57. #! For backwards compatibility it still assumes sizes of 16 and less to
  58. #! be specified in MB.
  59. #! * The suffixes k and M can now be used with all sizes and offsets to
  60. #! specify them in kilo or mega.
  61. #!
  62. #! Revision 1.12 2002/08/15 15:27:34 pkj
  63. #! Use $opts{'x'} instead of $opt_x.
  64. #!
  65. #! Revision 1.11 2002/07/04 17:06:39 pkj
  66. #! * No longer specifies a bootfile by default (not needed any longer).
  67. #! * Implemented option -b to specify a bootfile.
  68. #! * Removed references to option -l (it was never implemented).
  69. #!
  70. #! Revision 1.10 2002/06/04 11:50:23 starvik
  71. #! Check if mrs_data is specified in kernelconfig (necessary for MCM)
  72. #!
  73. #! Revision 1.9 2002/01/29 10:38:26 pkj
  74. #! Change illegal to invalid.
  75. #!
  76. #! Revision 1.8 2001/09/13 12:32:10 pkj
  77. #! * Added option -S to specify the size of the flash (in MB), as -s
  78. #! is used to specify how much to flash nowadays.
  79. #! * Made the default size of the flash depend on the size of the image
  80. #! file. If it is bigger than 0x200100 then the flash is assumed to
  81. #! be 4 MB, otherwise it is assumed to be 2 MB.
  82. #! * Added verification of various options.
  83. #!
  84. #! Revision 1.7 2001/09/13 10:25:11 pkj
  85. #! Minor clean-up.
  86. #!
  87. #! Revision 1.6 2001/06/29 10:05:16 pkj
  88. #! Corrected check for SDRAM.
  89. #!
  90. #! Revision 1.5 2001/06/29 09:11:55 pkj
  91. #! Synchronised boot_elinux and boot_linux.
  92. #!
  93. #!----------------------------------------------------------------------------
  94. #! (C) Copyright 2001, Axis Communications AB, LUND, SWEDEN
  95. #!****************************************************************************
  96. # $Id: boot_linux,v 1.16 2004/11/01 16:32:27 starvik Exp $
  97. #****************** INCLUDE FILES SECTION ************************************
  98. use strict;
  99. use Getopt::Std;
  100. use File::Basename;
  101. #****************** VARIABLE DECLARATION SECTION *****************************
  102. use vars qw($my_name %opts);
  103. use vars qw($text_start $cmd);
  104. use vars qw($image_name $image_size);
  105. use vars qw($offset $source_offset $flash_size $flashing_size);
  106. use vars qw($sdram_timing_address $sdram_config_address);
  107. use vars qw($sdram_precharge $sdram_nop $sdram_refresh $sdram_mrs);
  108. #****************** CONSTANT SECTION *****************************************
  109. # Register addresses
  110. $sdram_timing_address = "b0000008";
  111. $sdram_config_address = "b000000c";
  112. # SDRAM commands
  113. $sdram_precharge = 3;
  114. $sdram_nop = 0;
  115. $sdram_refresh = 2;
  116. $sdram_mrs = 1;
  117. #****************** MAIN PROGRAM SECTION *************************************
  118. # The name of this program.
  119. $my_name = basename($0);
  120. # Get options
  121. getopts('b:d:fFhi:o:O:ps:S:', \%opts);
  122. &print_help if ($opts{'h'});
  123. # Name and existance of the image
  124. $image_name = ($opts{'i'} ? $opts{'i'} : 'fimage');
  125. die "Could not find the image $image_name!\n" unless (-s $image_name);
  126. if ($opts{'f'} || $opts{'F'})
  127. {
  128. $image_size = -s $image_name;
  129. $offset = ($opts{'f'} ? 0x10000 : 0);
  130. $offset = &convert_size($opts{'o'}) if (defined($opts{'o'}));
  131. die("$my_name: Invalid destination offset\n") if ($offset !~ /^\d+$/);
  132. my $base_name = basename($image_name);
  133. if ($base_name eq 'timage' || $base_name eq 'flash1.img')
  134. {
  135. $source_offset = 0;
  136. }
  137. else
  138. {
  139. $source_offset = $offset;
  140. }
  141. $source_offset = &convert_size($opts{'O'}) if (defined($opts{'O'}));
  142. die("$my_name: Invalid source offset\n") if ($source_offset !~ /^\d+$/);
  143. die("$my_name: Source offset > image size\n") if ($source_offset > $image_size);
  144. if (defined($opts{'S'}))
  145. {
  146. # Backwards compatibility to allow specifying the flash size in MB
  147. # without using an M suffix
  148. $opts{'S'} .= 'M' if ($opts{'S'} =~ /^\d+$/ && $opts{'S'} <= 16);
  149. $flash_size = &convert_size($opts{'S'});
  150. }
  151. else
  152. {
  153. # Calculate a flash size large enough for the image without the checksum
  154. # and HWID.
  155. $flash_size = ($image_size - $source_offset + $offset) & 0xFFFF0000;
  156. }
  157. die("$my_name: Invalid flash size\n") if ($flash_size !~ /^\d+$/);
  158. die("$my_name: Destination offset > flash size\n") if ($offset > $flash_size);
  159. if (defined($opts{'s'}))
  160. {
  161. $flashing_size = &convert_size($opts{'s'});
  162. }
  163. else
  164. {
  165. $flashing_size = $flash_size - $offset;
  166. }
  167. die("$my_name: Invalid size to flash\n") if ($flashing_size !~ /^\d+$/);
  168. if ($flashing_size > $flash_size - $offset)
  169. {
  170. $flashing_size = $flash_size - $offset;
  171. 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);
  172. }
  173. if ($flashing_size > $image_size - $source_offset)
  174. {
  175. $flashing_size = $image_size - $source_offset;
  176. 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);
  177. }
  178. }
  179. # Create the command line to boot the image
  180. if (system('./etrax100boot --help > /dev/null') == 0)
  181. {
  182. $cmd = './etrax100boot';
  183. }
  184. elsif (system('svinto_boot --help > /dev/null') == 0)
  185. {
  186. $cmd = 'svinto_boot';
  187. }
  188. else
  189. {
  190. die("Cannot find e100boot program in your PATH!\n");
  191. }
  192. $cmd .= " --device $opts{'d'}" if ($opts{'d'});
  193. $cmd .= &extract_hw_settings;
  194. $cmd .= " --bootfile $opts{'b'}" if ($opts{'b'});
  195. $cmd .= " --file $image_name $text_start";
  196. if ($opts{'f'} || $opts{'F'})
  197. {
  198. $cmd .= sprintf(" --flash %lx %lx %lx --jump 0",
  199. hex($text_start) + $source_offset, $offset, $flashing_size);
  200. }
  201. else
  202. {
  203. $cmd .= " --jump $text_start";
  204. }
  205. if ($opts{'p'})
  206. {
  207. print "Command:\n$cmd\n";
  208. }
  209. else
  210. {
  211. system($cmd);
  212. }
  213. exit 0;
  214. #****************** FUNCTION DEFINITION SECTION ******************************
  215. #*****************************************************************************
  216. ##
  217. ## FUNCTION NAME: convert_size
  218. ##
  219. ##****************************************************************************
  220. sub convert_size
  221. {
  222. my($arg) = @_;
  223. my $size;
  224. if ($arg =~ /^0x([\da-fA-F]+)([kM])?$/)
  225. {
  226. $size = hex($1);
  227. }
  228. elsif ($arg =~ /^(\d+)([kM])?$/)
  229. {
  230. $size = $1;
  231. }
  232. else
  233. {
  234. return -1;
  235. }
  236. if (!defined($2))
  237. {
  238. return $size;
  239. }
  240. elsif ($2 eq 'k')
  241. {
  242. return $size * 1024;
  243. }
  244. elsif ($2 eq 'M')
  245. {
  246. return $size * 1048576;
  247. }
  248. }
  249. #*****************************************************************************
  250. ##
  251. ## FUNCTION NAME: extract_hw_settings
  252. ##
  253. ##****************************************************************************
  254. sub extract_hw_settings
  255. {
  256. my $data;
  257. my $dbg_port;
  258. my $sdram_enabled;
  259. my $return_value = "";
  260. my $sdram_config;
  261. # The hw information table has the following format
  262. #
  263. # "HW_PARAM_MAGIC"
  264. # text_start (dword)
  265. # serial debg port (dword)
  266. # sdram enabled (dword)
  267. # register address (dword)
  268. # register value (dword)
  269. # ...
  270. # 0
  271. open(FILE, "$image_name") || die("Could not open '$image_name'");
  272. while (<FILE>)
  273. {
  274. if (m/HW_PARAM_MAGIC/g)
  275. {
  276. # Seek to first byte after magic
  277. seek(FILE, -length($_) + pos($_), 1);
  278. last;
  279. }
  280. }
  281. $text_start = &get_dword;
  282. $dbg_port = &get_dword;
  283. $sdram_enabled = int(&get_dword);
  284. while (1)
  285. {
  286. my $register = &get_dword;
  287. my $value = &get_dword;
  288. last if ($register eq "00000000");
  289. if ($sdram_enabled)
  290. {
  291. if ($register eq $sdram_config_address)
  292. {
  293. $sdram_config = $value;
  294. }
  295. elsif ($register eq $sdram_timing_address)
  296. {
  297. $return_value .= &calculate_sdram_init($value, $sdram_config);
  298. next;
  299. }
  300. }
  301. $return_value .= " --setreg $register $value";
  302. }
  303. close(FILE);
  304. return $return_value;
  305. }
  306. #*****************************************************************************
  307. ##
  308. ## FUNCTION NAME: get_dword
  309. ##
  310. ##****************************************************************************
  311. sub get_dword
  312. {
  313. my $data;
  314. read(FILE, $data, 4);
  315. return unpack("H8", pack("V", unpack("N", $data)));
  316. }
  317. #*****************************************************************************
  318. ##
  319. ## FUNCTION NAME: calculate_sdram_init
  320. ##
  321. ##****************************************************************************
  322. sub calculate_sdram_init
  323. {
  324. # Refer to ETRAX 100LX Designers Reference for a description of SDRAM
  325. # initialization
  326. my $sdram_init_val = hex($_[0]);
  327. my $sdram_config_val = hex($_[1]);
  328. my $bus_width = $sdram_config_val & 0x00800000;
  329. my $speed;
  330. my $cas_latency;
  331. my $mrs_data;
  332. my $temp;
  333. my $return_value;
  334. my $value;
  335. $mrs_data = ($sdram_init_val & 0x00ff0000) >> 16;
  336. $sdram_init_val &= 0x8000ffff; # Make sure mrs data is 0
  337. $sdram_init_val |= 0x80000000; # Make sure sdram is enabled
  338. $speed = $sdram_init_val & 0x1000;
  339. $cas_latency = $sdram_init_val & 0x3;
  340. if ($speed) # 100 MHz
  341. {
  342. $cas_latency += 2;
  343. }
  344. else # 50 MHz
  345. {
  346. $cas_latency += 1;
  347. }
  348. # Calculate value of mrs_data
  349. # CAS latency = 2 && bus_width = 32 => 0x40
  350. # CAS latency = 3 && bus_width = 32 => 0x60
  351. # CAS latency = 2 && bus_width = 16 => 0x20
  352. # CAS latency = 3 && bus_width = 16 => 0x30
  353. if ($mrs_data == 0)
  354. {
  355. if ($bus_width == 0) # 16 bits
  356. {
  357. $mrs_data = $cas_latency == 2 ? 0x20 : 0x30;
  358. }
  359. else # 32 bits
  360. {
  361. $mrs_data = $cas_latency == 2 ? 0x40 : 0x60;
  362. }
  363. }
  364. $temp = $sdram_init_val | 0x0000c000; # Disable refresh
  365. $return_value .= &sdram_command($temp);
  366. $return_value .= " --pause 20000";
  367. $return_value .= &sdram_command($temp, $sdram_precharge);
  368. $return_value .= &sdram_command($temp, $sdram_nop);
  369. $return_value .= " --setreg +0 7";
  370. $return_value .= " --label label1";
  371. $return_value .= &sdram_command($temp, $sdram_refresh);
  372. $return_value .= &sdram_command($temp, $sdram_nop);
  373. $return_value .= " --loop +0 label1";
  374. $return_value .= &sdram_command($temp, $sdram_mrs, $mrs_data);
  375. $return_value .= &sdram_command($temp, $sdram_nop);
  376. $return_value .= &sdram_command($sdram_init_val);
  377. return $return_value;
  378. }
  379. #*****************************************************************************
  380. ##
  381. ## FUNCTION NAME: sdram_command
  382. ##
  383. ##****************************************************************************
  384. sub sdram_command
  385. {
  386. my($temp, $value, $mrs_data) = @_;
  387. $value ||= 0;
  388. if ($value == $sdram_mrs)
  389. {
  390. $value = sprintf("%lx", $temp | ($value << 9) | ($mrs_data << 16));
  391. }
  392. else
  393. {
  394. $value = sprintf("%lx", $temp | ($value << 9));
  395. }
  396. return " --setreg $sdram_timing_address $value";
  397. }
  398. #*****************************************************************************
  399. ##
  400. ## FUNCTION NAME: print_help
  401. ##
  402. ##****************************************************************************
  403. sub print_help
  404. {
  405. print "\nAXIS $my_name, ", '$Revision: 1.16 $ $Date: 2004/11/01 16:32:27 $ ', "\n";
  406. die <<EOT;
  407. Copyright (C) 2001-2002 Axis Communications AB
  408. DESCRIPTION:
  409. This program is used to boot (and flash) a linux image to a box.
  410. It tries to extract the required ETRAX 100 settings from the image file.
  411. SYNTAX:
  412. $my_name [options]
  413. OPTIONS:
  414. -b <bootfile> : The boot image to use.
  415. -d <device> : The network interface to use, default is eth0.
  416. -f : Save the image in the flash memory starting at
  417. address 0x10000.
  418. -F : Save the image in the flash memory starting at
  419. address 0.
  420. -h : Print this help text.
  421. -i <image> : The path and name of the image to use, default
  422. is fimage.
  423. -o <offset> : The offset in the flash where the flashing starts.
  424. -O <offset> : The offset in the image file where the flashing
  425. starts from.
  426. -p : Print the resulting etrax100boot command instead
  427. of executing it.
  428. -s <size> : How much to flash (default is the size of the
  429. flash minus the offset specified using -o or -f).
  430. -S <size> : The size of the flash.
  431. All sizes and offsets above can be specified as decimal numbers, or as
  432. hexadecimal numbers by prefixing them with 0x. It is also possible to use
  433. the suffixes k and M to specify kilo (1024) or mega (1048576).
  434. EOT
  435. }
  436. #****************** END OF FILE boot_linux ***********************************