update Chinese(Simplified) translation
[maemo-rb.git] / utils / imxtools / sbtools / sb1.h
blob2adfc6a26cb88b693789ef81e1a3eb880bc7be55
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2012 Amaury Pouly
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #ifndef __SB1_H__
22 #define __SB1_H__
24 #include <stdint.h>
25 #include <stdbool.h>
27 #include "misc.h"
29 #define SECTOR_SIZE 512
31 /* All fields are in big-endian BCD */
32 struct sb1_version_t
34 uint16_t major;
35 uint16_t pad0;
36 uint16_t minor;
37 uint16_t pad1;
38 uint16_t revision;
39 uint16_t pad2;
42 struct sb1_header_t
44 uint32_t rom_version;
45 uint32_t image_size;
46 uint32_t header_size;
47 uint32_t userdata_offset;
48 uint32_t pad2;
49 uint8_t signature[4]; /* Signature "STMP" */
50 struct sb1_version_t product_ver;
51 struct sb1_version_t component_ver;
52 uint32_t drive_tag;
53 } __attribute__((packed));
55 struct sb1_cmd_header_t
57 uint32_t cmd; // 31:21=cmd size, 20=critical, 19:6=size 5:4=datatype, 3:0=boot cmd
58 uint32_t addr;
59 } __attribute__((packed));
61 #define SB1_CMD_MAX_LOAD_SIZE 0x1ff8
62 #define SB1_CMD_MAX_FILL_SIZE 0x3fff
64 #define SB1_CMD_SIZE(cmd) ((cmd) >> 21)
65 #define SB1_CMD_CRITICAL(cmd) !!(cmd & (1 << 20))
66 #define SB1_CMD_BYTES(cmd) (((cmd) >> 6) & 0x3fff)
67 #define SB1_CMD_DATATYPE(cmd) (((cmd) >> 4) & 0x3)
68 #define SB1_CMD_BOOT(cmd) ((cmd) & 0xf)
70 #define SB1_MK_CMD(boot,data,bytes,crit,size) \
71 ((boot) | (data) << 4 | (bytes) << 6 | (crit) << 20 | (size) << 21)
73 #define SB1_ADDR_SDRAM_CS(addr) ((addr) & 0x3)
74 #define SB1_ADDR_SDRAM_SZ(addr) ((addr) >> 16)
76 #define SB1_MK_ADDR_SDRAM(cs,sz) ((cs) | (sz) << 16)
78 int sb1_sdram_size_by_index(int index); // returns - 1 on error
79 int sb1_sdram_index_by_size(int size); // returns -1 on error
81 #define SB1_INST_LOAD 0x1
82 #define SB1_INST_FILL 0x2
83 #define SB1_INST_JUMP 0x3
84 #define SB1_INST_CALL 0x4
85 #define SB1_INST_MODE 0x5
86 #define SB1_INST_SDRAM 0x6
88 #define SB1_DATATYPE_UINT32 0
89 #define SB1_DATATYPE_UINT16 1
90 #define SB1_DATATYPE_UINT8 2
92 /*******
93 * API *
94 *******/
96 struct sb1_inst_t
98 uint8_t cmd;
99 uint16_t size;
100 // <union>
101 struct
103 uint8_t chip_select;
104 uint8_t size_index;
105 }sdram;
106 uint8_t mode;
107 uint32_t addr;
108 // </union>
109 uint8_t datatype;
110 uint8_t critical;
111 // <union>
112 void *data;
113 uint32_t pattern;
114 uint32_t argument;
115 // </union>
118 struct sb1_file_t
120 uint32_t rom_version;
121 uint32_t pad2; // unknown meaning but copy it anyway !
122 uint32_t drive_tag;
123 struct sb1_version_t product_ver;
124 struct sb1_version_t component_ver;
125 int nr_insts;
126 struct sb1_inst_t *insts;
127 void *userdata;
128 int userdata_size;
129 struct crypto_key_t key;
132 enum sb1_error_t
134 SB1_SUCCESS = 0,
135 SB1_ERROR = -1,
136 SB1_OPEN_ERROR = -2,
137 SB1_READ_ERROR = -3,
138 SB1_WRITE_ERROR = -4,
139 SB1_FORMAT_ERROR = -5,
140 SB1_CHECKSUM_ERROR = -6,
141 SB1_NO_VALID_KEY = -7,
142 SB1_FIRST_CRYPTO_ERROR = -8,
143 SB1_LAST_CRYPTO_ERROR = SB1_FIRST_CRYPTO_ERROR - CRYPTO_NUM_ERRORS,
146 enum sb1_error_t sb1_write_file(struct sb1_file_t *sb, const char *filename);
148 typedef void (*sb1_color_printf)(void *u, bool err, color_t c, const char *f, ...);
149 struct sb1_file_t *sb1_read_file(const char *filename, void *u,
150 sb1_color_printf printf, enum sb1_error_t *err);
151 /* use size_t(-1) to use maximum size */
152 struct sb1_file_t *sb1_read_file_ex(const char *filename, size_t offset, size_t size,
153 void *u, sb1_color_printf printf, enum sb1_error_t *err);
154 struct sb1_file_t *sb1_read_memory(void *buffer, size_t size, void *u,
155 sb1_color_printf printf, enum sb1_error_t *err);
157 /* do as little checks as possible, make sure the image is valid (advance use only) */
158 bool sb1_is_key_valid_fast(void *buffer, size_t size, union xorcrypt_key_t key[2]);
159 bool sb1_brute_force(const char *filename, void *u, sb1_color_printf printf,
160 enum sb1_error_t *err, struct crypto_key_t *key);
162 void sb1_get_default_key(struct crypto_key_t *key);
164 void sb1_dump(struct sb1_file_t *file, void *u, sb1_color_printf printf);
165 void sb1_free(struct sb1_file_t *file);
167 #endif /* __SB1_H__ */