1
0

boot_linux 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. #****************** INCLUDE FILES SECTION ************************************
  97. use strict;
  98. use Getopt::Std;
  99. use File::Basename;
  100. #****************** VARIABLE DECLARATION SECTION *****************************
  101. use vars qw($my_name %opts);
  102. use vars qw($text_start $cmd);
  103. use vars qw($image_name $image_size);
  104. use vars qw($offset $source_offset $flash_size $flashing_size);
  105. use vars qw($sdram_timing_address $sdram_config_address);
  106. use vars qw($sdram_precharge $sdram_nop $sdram_refresh $sdram_mrs);
  107. #****************** CONSTANT SECTION *****************************************
  108. # Register addresses
  109. $sdram_timing_address = "b0000008";
  110. $sdram_config_address = "b000000c";
  111. # SDRAM commands
  112. $sdram_precharge = 3;
  113. $sdram_nop = 0;
  114. $sdram_refresh = 2;
  115. $sdram_mrs = 1;
  116. #****************** MAIN PROGRAM SECTION *************************************
  117. # The name of this program.
  118. $my_name = basename($0);
  119. # Get options
  120. getopts('b:d:fFhi:o:O:ps:S:', \%opts);
  121. &print_help if ($opts{'h'});
  122. # Name and existance of the image
  123. $image_name = ($opts{'i'} ? $opts{'i'} : 'fimage');
  124. die "Could not find the image $image_name!\n" unless (-s $image_name);
  125. if ($opts{'f'} || $opts{'F'})
  126. {
  127. $image_size = -s $image_name;
  128. $offset = ($opts{'f'} ? 0x10000 : 0);
  129. $offset = &convert_size($opts{'o'}) if (defined($opts{'o'}));
  130. die("$my_name: Invalid destination offset\n") if ($offset !~ /^\d+$/);
  131. my $base_name = basename($image_name);
  132. if ($base_name eq 'timage' || $base_name eq 'flash1.img')
  133. {
  134. $source_offset = 0;
  135. }
  136. else
  137. {
  138. $source_offset = $offset;
  139. }
  140. $source_offset = &convert_size($opts{'O'}) if (defined($opts{'O'}));
  141. die("$my_name: Invalid source offset\n") if ($source_offset !~ /^\d+$/);
  142. die("$my_name: Source offset > image size\n") if ($source_offset > $image_size);
  143. if (defined($opts{'S'}))
  144. {
  145. # Backwards compatibility to allow specifying the flash size in MB
  146. # without using an M suffix
  147. $opts{'S'} .= 'M' if ($opts{'S'} =~ /^\d+$/ && $opts{'S'} <= 16);
  148. $flash_size = &convert_size($opts{'S'});
  149. }
  150. else
  151. {
  152. # Calculate a flash size large enough for the image without the checksum
  153. # and HWID.
  154. $flash_size = ($image_size - $source_offset + $offset) & 0xFFFF0000;
  155. }
  156. die("$my_name: Invalid flash size\n") if ($flash_size !~ /^\d+$/);
  157. die("$my_name: Destination offset > flash size\n") if ($offset > $flash_size);
  158. if (defined($opts{'s'}))
  159. {
  160. $flashing_size = &convert_size($opts{'s'});
  161. }
  162. else
  163. {
  164. $flashing_size = $flash_size - $offset;
  165. }
  166. die("$my_name: Invalid size to flash\n") if ($flashing_size !~ /^\d+$/);
  167. if ($flashing_size > $flash_size - $offset)
  168. {
  169. $flashing_size = $flash_size - $offset;
  170. 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);
  171. }
  172. if ($flashing_size > $image_size - $source_offset)
  173. {
  174. $flashing_size = $image_size - $source_offset;
  175. 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);
  176. }
  177. }
  178. # Create the command line to boot the image
  179. if (system('./etrax100boot --help > /dev/null') == 0)
  180. {
  181. $cmd = './etrax100boot';
  182. }
  183. elsif (system('svinto_boot --help > /dev/null') == 0)
  184. {
  185. $cmd = 'svinto_boot';
  186. }
  187. else
  188. {
  189. die("Cannot find e100boot program in your PATH!\n");
  190. }
  191. $cmd .= " --device $opts{'d'}" if ($opts{'d'});
  192. $cmd .= &extract_hw_settings;
  193. $cmd .= " --bootfile $opts{'b'}" if ($opts{'b'});
  194. $cmd .= " --file $image_name $text_start";
  195. if ($opts{'f'} || $opts{'F'})
  196. {
  197. $cmd .= sprintf(" --flash %lx %lx %lx --jump 0",
  198. hex($text_start) + $source_offset, $offset, $flashing_size);
  199. }
  200. else
  201. {
  202. $cmd .= " --jump $text_start";
  203. }
  204. if ($opts{'p'})
  205. {
  206. print "Command:\n$cmd\n";
  207. }
  208. else
  209. {
  210. system($cmd);
  211. }
  212. exit 0;
  213. #****************** FUNCTION DEFINITION SECTION ******************************
  214. #*****************************************************************************
  215. ##
  216. ## FUNCTION NAME: convert_size
  217. ##
  218. ##****************************************************************************
  219. sub convert_size
  220. {
  221. my($arg) = @_;
  222. my $size;
  223. if ($arg =~ /^0x([\da-fA-F]+)([kM])?$/)
  224. {
  225. $size = hex($1);
  226. }
  227. elsif ($arg =~ /^(\d+)([kM])?$/)
  228. {
  229. $size = $1;
  230. }
  231. else
  232. {
  233. return -1;
  234. }
  235. if (!defined($2))
  236. {
  237. return $size;
  238. }
  239. elsif ($2 eq 'k')
  240. {
  241. return $size * 1024;
  242. }
  243. elsif ($2 eq 'M')
  244. {
  245. return $size * 1048576;
  246. }
  247. }
  248. #*****************************************************************************
  249. ##
  250. ## FUNCTION NAME: extract_hw_settings
  251. ##
  252. ##****************************************************************************
  253. sub extract_hw_settings
  254. {
  255. my $data;
  256. my $dbg_port;
  257. my $sdram_enabled;
  258. my $return_value = "";
  259. my $sdram_config;
  260. # The hw information table has the following format
  261. #
  262. # "HW_PARAM_MAGIC"
  263. # text_start (dword)
  264. # serial debg port (dword)
  265. # sdram enabled (dword)
  266. # register address (dword)
  267. # register value (dword)
  268. # ...
  269. # 0
  270. open(FILE, "$image_name") || die("Could not open '$image_name'");
  271. while (<FILE>)
  272. {
  273. if (m/HW_PARAM_MAGIC/g)
  274. {
  275. # Seek to first byte after magic
  276. seek(FILE, -length($_) + pos($_), 1);
  277. last;
  278. }
  279. }
  280. $text_start = &get_dword;
  281. $dbg_port = &get_dword;
  282. $sdram_enabled = int(&get_dword);
  283. while (1)
  284. {
  285. my $register = &get_dword;
  286. my $value = &get_dword;
  287. last if ($register eq "00000000");
  288. if ($sdram_enabled)
  289. {
  290. if ($register eq $sdram_config_address)
  291. {
  292. $sdram_config = $value;
  293. }
  294. elsif ($register eq $sdram_timing_address)
  295. {
  296. $return_value .= &calculate_sdram_init($value, $sdram_config);
  297. next;
  298. }
  299. }
  300. $return_value .= " --setreg $register $value";
  301. }
  302. close(FILE);
  303. return $return_value;
  304. }
  305. #*****************************************************************************
  306. ##
  307. ## FUNCTION NAME: get_dword
  308. ##
  309. ##****************************************************************************
  310. sub get_dword
  311. {
  312. my $data;
  313. read(FILE, $data, 4);
  314. return unpack("H8", pack("V", unpack("N", $data)));
  315. }
  316. #*****************************************************************************
  317. ##
  318. ## FUNCTION NAME: calculate_sdram_init
  319. ##
  320. ##****************************************************************************
  321. sub calculate_sdram_init
  322. {
  323. # Refer to ETRAX 100LX Designers Reference for a description of SDRAM
  324. # initialization
  325. my $sdram_init_val = hex($_[0]);
  326. my $sdram_config_val = hex($_[1]);
  327. my $bus_width = $sdram_config_val & 0x00800000;
  328. my $speed;
  329. my $cas_latency;
  330. my $mrs_data;
  331. my $temp;
  332. my $return_value;
  333. my $value;
  334. $mrs_data = ($sdram_init_val & 0x00ff0000) >> 16;
  335. $sdram_init_val &= 0x8000ffff; # Make sure mrs data is 0
  336. $sdram_init_val |= 0x80000000; # Make sure sdram is enabled
  337. $speed = $sdram_init_val & 0x1000;
  338. $cas_latency = $sdram_init_val & 0x3;
  339. if ($speed) # 100 MHz
  340. {
  341. $cas_latency += 2;
  342. }
  343. else # 50 MHz
  344. {
  345. $cas_latency += 1;
  346. }
  347. # Calculate value of mrs_data
  348. # CAS latency = 2 && bus_width = 32 => 0x40
  349. # CAS latency = 3 && bus_width = 32 => 0x60
  350. # CAS latency = 2 && bus_width = 16 => 0x20
  351. # CAS latency = 3 && bus_width = 16 => 0x30
  352. if ($mrs_data == 0)
  353. {
  354. if ($bus_width == 0) # 16 bits
  355. {
  356. $mrs_data = $cas_latency == 2 ? 0x20 : 0x30;
  357. }
  358. else # 32 bits
  359. {
  360. $mrs_data = $cas_latency == 2 ? 0x40 : 0x60;
  361. }
  362. }
  363. $temp = $sdram_init_val | 0x0000c000; # Disable refresh
  364. $return_value .= &sdram_command($temp);
  365. $return_value .= " --pause 20000";
  366. $return_value .= &sdram_command($temp, $sdram_precharge);
  367. $return_value .= &sdram_command($temp, $sdram_nop);
  368. $return_value .= " --setreg +0 7";
  369. $return_value .= " --label label1";
  370. $return_value .= &sdram_command($temp, $sdram_refresh);
  371. $return_value .= &sdram_command($temp, $sdram_nop);
  372. $return_value .= " --loop +0 label1";
  373. $return_value .= &sdram_command($temp, $sdram_mrs, $mrs_data);
  374. $return_value .= &sdram_command($temp, $sdram_nop);
  375. $return_value .= &sdram_command($sdram_init_val);
  376. return $return_value;
  377. }
  378. #*****************************************************************************
  379. ##
  380. ## FUNCTION NAME: sdram_command
  381. ##
  382. ##****************************************************************************
  383. sub sdram_command
  384. {
  385. my($temp, $value, $mrs_data) = @_;
  386. $value ||= 0;
  387. if ($value == $sdram_mrs)
  388. {
  389. $value = sprintf("%lx", $temp | ($value << 9) | ($mrs_data << 16));
  390. }
  391. else
  392. {
  393. $value = sprintf("%lx", $temp | ($value << 9));
  394. }
  395. return " --setreg $sdram_timing_address $value";
  396. }
  397. #*****************************************************************************
  398. ##
  399. ## FUNCTION NAME: print_help
  400. ##
  401. ##****************************************************************************
  402. sub print_help
  403. {
  404. print "\nAXIS $my_name, ", '$Revision: 1.16 $ $Date: 2004/11/01 16:32:27 $ ', "\n";
  405. die <<EOT;
  406. Copyright (C) 2001-2002 Axis Communications AB
  407. DESCRIPTION:
  408. This program is used to boot (and flash) a linux image to a box.
  409. It tries to extract the required ETRAX 100 settings from the image file.
  410. SYNTAX:
  411. $my_name [options]
  412. OPTIONS:
  413. -b <bootfile> : The boot image to use.
  414. -d <device> : The network interface to use, default is eth0.
  415. -f : Save the image in the flash memory starting at
  416. address 0x10000.
  417. -F : Save the image in the flash memory starting at
  418. address 0.
  419. -h : Print this help text.
  420. -i <image> : The path and name of the image to use, default
  421. is fimage.
  422. -o <offset> : The offset in the flash where the flashing starts.
  423. -O <offset> : The offset in the image file where the flashing
  424. starts from.
  425. -p : Print the resulting etrax100boot command instead
  426. of executing it.
  427. -s <size> : How much to flash (default is the size of the
  428. flash minus the offset specified using -o or -f).
  429. -S <size> : The size of the flash.
  430. All sizes and offsets above can be specified as decimal numbers, or as
  431. hexadecimal numbers by prefixing them with 0x. It is also possible to use
  432. the suffixes k and M to specify kilo (1024) or mega (1048576).
  433. EOT
  434. }
  435. #****************** END OF FILE boot_linux ***********************************