xref: /rk3399_ARM-atf/tools/renesas/rza/rza3m/rz_image.pl (revision 66a0bb47058db8a4f74ccc1543a146094829e110)
1#!/usr/bin/perl -w
2# Copyright (c) 2021-2026, Renesas Electronics Corporation. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5# -----------------------------------------------------------------------------
6# PURPOSE
7#   This script prepends a header to the BL2 image to support Boot Mode 3 (Quad Serial NOR Flash)
8#
9# EXTERNAL FLASH LAYOUT
10#                                                          Offset
11#     Header     +-----------------------------+           0x0000
12#   (512 bytes)  | BL2 size                    |
13#                +-----------------------------+           0x0004
14#                | ...                         |
15#                | <filled with 0xff>          |
16#                | ...                         |
17#                +-----------------------------+           0x01c0
18#                | 0xffff0000                  | (pattern to support timing adjustment for
19#                | 0x000800ff                  | serial flash at DDR mode following Figure 25.20
20#                | 0x00fff700                  | RZ/A3M Group User’s Manual: Hardware Rev 1.05)
21#                | 0xf700f708                  |
22#                +-----------------------------+           0x01d0
23#                | ...                         |
24#                | <filled with 0xff>          |
25#                | ...                         |
26#                +-----------------------------+           0x01fc
27#                | 0xffff55aa (signature)      |
28# Loader program +-----------------------------+           0x0200
29#     (BL2)      | BL2 image                   |
30#                | Optional 0x80 then zeros    | (to reach 256-byte boundary)
31#                | 256-byte padding block      |
32#                +-----------------------------+
33
34use strict;
35use bigint;
36my $size_limit = 0x1D000;
37
38die("Not enough parameter\n") if ($#ARGV < 0);
39
40# Open input file
41my $name = shift(@ARGV);
42my $outname;
43if ($#ARGV < 0) {
44	$outname = "rz_" . $name;
45}
46else {
47	$outname = shift(@ARGV);
48}
49open(my $origin, '<', $name) or die("Can not open input file");
50binmode $origin;
51
52# Obtaining size ($st[7])
53my @st = stat($origin);
54
55# Check appended size
56my $size = ($st[7] + 3) & "0xfffffffffffffffc";
57my $msg;
58if ($size != $st[7]) {$msg = "Appended size";} else {$msg="Size";}
59die("$msg too big ($size > $size_limit)") if ($size > $size_limit);
60
61# Create temporary file
62open(my $out, '>', $outname) or die("Can not open output file");
63binmode $out;
64
65# Write header
66$out->print(pack('L', $size));
67for(my $i = 1; $i < 112; $i++) {
68	$out->print(pack('L', 0xffffffff));
69}
70$out->print(pack('L', 0xffff0000));
71$out->print(pack('L', 0x000800ff));
72$out->print(pack('L', 0x00fff700));
73$out->print(pack('L', 0xf700f708));
74$out->print(pack('L', 0xffffffff));
75$out->print(pack('L', 0xffffffff));
76$out->print(pack('L', 0xffffffff));
77$out->print(pack('L', 0xffffffff));
78$out->print(pack('L', 0xffffffff));
79$out->print(pack('L', 0xffffffff));
80$out->print(pack('L', 0xffffffff));
81$out->print(pack('L', 0xffffffff));
82$out->print(pack('L', 0xffffffff));
83$out->print(pack('L', 0xffffffff));
84$out->print(pack('L', 0xffffffff));
85$out->print(pack('L', 0xaa55ffff));
86
87# Append original data to temporary file
88my $buf;
89read($origin, $buf, $st[7]);
90$out->print($buf);
91if($st[7] < $size) {
92	warn "Not aligned. Append " . ($size-$st[7]) . " zero(s)";
93	# Append zero
94	for(;$st[7] < $size; $size--) {
95		$out->print(pack('C', 0));
96	}
97}
98$out->flush;
99
100# close
101close $out;
102close $origin;
103