summaryrefslogtreecommitdiff
path: root/utils/imxtools/sbtools
diff options
context:
space:
mode:
authorAmaury Pouly <amaury.pouly@gmail.com>2012-12-16 21:07:31 +0100
committerAmaury Pouly <amaury.pouly@gmail.com>2012-12-16 21:28:41 +0100
commit212cfdf771dc0059804cf817caaf2e43ef2b3f50 (patch)
tree48606c29d82efb075fff71789155719cab3dd6da /utils/imxtools/sbtools
parentf4f600fc52d38e4a287913050880647144d1873c (diff)
downloadrockbox-212cfdf771dc0059804cf817caaf2e43ef2b3f50.tar.gz
rockbox-212cfdf771dc0059804cf817caaf2e43ef2b3f50.zip
imxtools/sbtools: fix some instruction handling & crypto
Change-Id: I6530bdf27896d8325dec4e2ba31c7e6a0131a286
Diffstat (limited to 'utils/imxtools/sbtools')
-rw-r--r--utils/imxtools/sbtools/sb1.c20
-rw-r--r--utils/imxtools/sbtools/sb1.h5
2 files changed, 17 insertions, 8 deletions
diff --git a/utils/imxtools/sbtools/sb1.c b/utils/imxtools/sbtools/sb1.c
index 576196d5bd..6149ef37c2 100644
--- a/utils/imxtools/sbtools/sb1.c
+++ b/utils/imxtools/sbtools/sb1.c
@@ -59,6 +59,8 @@ static void fix_version(struct sb1_version_t *ver)
59 59
60enum sb1_error_t sb1_write_file(struct sb1_file_t *sb, const char *filename) 60enum sb1_error_t sb1_write_file(struct sb1_file_t *sb, const char *filename)
61{ 61{
62 if(sb->key.method != CRYPTO_XOR_KEY)
63 return SB1_NO_VALID_KEY;
62 /* compute image size (without userdata) */ 64 /* compute image size (without userdata) */
63 uint32_t image_size = 0; 65 uint32_t image_size = 0;
64 image_size += sizeof(struct sb1_header_t); 66 image_size += sizeof(struct sb1_header_t);
@@ -104,6 +106,7 @@ enum sb1_error_t sb1_write_file(struct sb1_file_t *sb, const char *filename)
104 for(int i = 0; i < sb->nr_insts; i++) 106 for(int i = 0; i < sb->nr_insts; i++)
105 { 107 {
106 int bytes = 0; 108 int bytes = 0;
109 int size = 0;
107 switch(sb->insts[i].cmd) 110 switch(sb->insts[i].cmd)
108 { 111 {
109 case SB1_INST_LOAD: 112 case SB1_INST_LOAD:
@@ -114,7 +117,8 @@ enum sb1_error_t sb1_write_file(struct sb1_file_t *sb, const char *filename)
114 bytes - sb->insts[i].size); 117 bytes - sb->insts[i].size);
115 break; 118 break;
116 case SB1_INST_FILL: 119 case SB1_INST_FILL:
117 bytes = 4; 120 bytes = sb->insts[i].size;
121 size = 2;
118 memcpy(cmd + 1, &sb->insts[i].pattern, 4); 122 memcpy(cmd + 1, &sb->insts[i].pattern, 4);
119 cmd->addr = sb->insts[i].addr; 123 cmd->addr = sb->insts[i].addr;
120 break; 124 break;
@@ -125,7 +129,7 @@ enum sb1_error_t sb1_write_file(struct sb1_file_t *sb, const char *filename)
125 memcpy(cmd + 1, &sb->insts[i].argument, 4); 129 memcpy(cmd + 1, &sb->insts[i].argument, 4);
126 break; 130 break;
127 case SB1_INST_MODE: 131 case SB1_INST_MODE:
128 bytes = 4; 132 bytes = 0;
129 cmd->addr = sb->insts[i].mode; 133 cmd->addr = sb->insts[i].mode;
130 break; 134 break;
131 case SB1_INST_SDRAM: 135 case SB1_INST_SDRAM:
@@ -137,11 +141,15 @@ enum sb1_error_t sb1_write_file(struct sb1_file_t *sb, const char *filename)
137 bugp("Unknown SB instruction: %#x\n", sb->insts[i].cmd); 141 bugp("Unknown SB instruction: %#x\n", sb->insts[i].cmd);
138 } 142 }
139 143
144 /* handle most common cases */
145 if(size == 0)
146 size = ROUND_UP(bytes, 4) / 4 + 1;
147
140 cmd->cmd = SB1_MK_CMD(sb->insts[i].cmd, sb->insts[i].datatype, 148 cmd->cmd = SB1_MK_CMD(sb->insts[i].cmd, sb->insts[i].datatype,
141 bytes, sb->insts[i].critical, 149 bytes, sb->insts[i].critical,
142 ROUND_UP(bytes, 4) / 4 + 1); 150 size);
143 151
144 cmd = (void *)cmd + 8 + ROUND_UP(bytes, 4); 152 cmd = (void *)cmd + 4 + size * 4;
145 } 153 }
146 154
147 /* move everything to prepare crypto marks (start at the end !) */ 155 /* move everything to prepare crypto marks (start at the end !) */
@@ -149,7 +157,7 @@ enum sb1_error_t sb1_write_file(struct sb1_file_t *sb, const char *filename)
149 memmove(buf + i * SECTOR_SIZE, buf + i * (SECTOR_SIZE - 4), SECTOR_SIZE - 4); 157 memmove(buf + i * SECTOR_SIZE, buf + i * (SECTOR_SIZE - 4), SECTOR_SIZE - 4);
150 158
151 union xorcrypt_key_t key[2]; 159 union xorcrypt_key_t key[2];
152 memcpy(key, sb->key, sizeof(sb->key)); 160 memcpy(key, sb->key.u.xor_key, sizeof(sb->key));
153 void *ptr = header + 1; 161 void *ptr = header + 1;
154 int offset = header->header_size; 162 int offset = header->header_size;
155 for(unsigned i = 0; i < image_size / SECTOR_SIZE; i++) 163 for(unsigned i = 0; i < image_size / SECTOR_SIZE; i++)
@@ -354,7 +362,7 @@ struct sb1_file_t *sb1_read_memory(void *_buf, size_t filesize, void *u,
354 } 362 }
355 } 363 }
356 364
357 memcpy(file->key, key, sizeof(key)); 365 memcpy(file->key.u.xor_key, key, sizeof(key));
358 366
359 if(!valid_key) 367 if(!valid_key)
360 fatal(SB1_NO_VALID_KEY, "No valid key found\n"); 368 fatal(SB1_NO_VALID_KEY, "No valid key found\n");
diff --git a/utils/imxtools/sbtools/sb1.h b/utils/imxtools/sbtools/sb1.h
index 0a0c9fe69c..1ee28978e1 100644
--- a/utils/imxtools/sbtools/sb1.h
+++ b/utils/imxtools/sbtools/sb1.h
@@ -58,7 +58,8 @@ struct sb1_cmd_header_t
58 uint32_t addr; 58 uint32_t addr;
59} __attribute__((packed)); 59} __attribute__((packed));
60 60
61#define SB1_CMD_MAX_SIZE 0x1ff8 61#define SB1_CMD_MAX_LOAD_SIZE 0x1ff8
62#define SB1_CMD_MAX_FILL_SIZE 0x3fff
62 63
63#define SB1_CMD_SIZE(cmd) ((cmd) >> 21) 64#define SB1_CMD_SIZE(cmd) ((cmd) >> 21)
64#define SB1_CMD_CRITICAL(cmd) !!(cmd & (1 << 20)) 65#define SB1_CMD_CRITICAL(cmd) !!(cmd & (1 << 20))
@@ -125,7 +126,7 @@ struct sb1_file_t
125 struct sb1_inst_t *insts; 126 struct sb1_inst_t *insts;
126 void *userdata; 127 void *userdata;
127 int userdata_size; 128 int userdata_size;
128 union xorcrypt_key_t key[2]; 129 struct crypto_key_t key;
129}; 130};
130 131
131enum sb1_error_t 132enum sb1_error_t