summaryrefslogtreecommitdiff
path: root/rbutil/jztool/src/identify_file.c
diff options
context:
space:
mode:
authorAidan MacDonald <amachronic@protonmail.com>2021-07-17 19:35:09 +0100
committerAidan MacDonald <amachronic@protonmail.com>2021-07-20 14:56:58 +0000
commit740a50687f67f3684e4b5698f8f30e3adebb8f6e (patch)
treecc2afcc989f0ca89e849338ad136e4042acd6bc1 /rbutil/jztool/src/identify_file.c
parent6f042e91dd8730bf72b423248aa2c520708f03c5 (diff)
downloadrockbox-740a50687f67f3684e4b5698f8f30e3adebb8f6e.tar.gz
rockbox-740a50687f67f3684e4b5698f8f30e3adebb8f6e.zip
jztool: add support for Shanling Q1 and Eros Qbootloader_shanlingq1_v1
Change-Id: I8e93162d1a9d82e9d132219f2803b1856d24ae6c
Diffstat (limited to 'rbutil/jztool/src/identify_file.c')
-rw-r--r--rbutil/jztool/src/identify_file.c37
1 files changed, 23 insertions, 14 deletions
diff --git a/rbutil/jztool/src/identify_file.c b/rbutil/jztool/src/identify_file.c
index e735075687..e475d98a3b 100644
--- a/rbutil/jztool/src/identify_file.c
+++ b/rbutil/jztool/src/identify_file.c
@@ -118,43 +118,52 @@ int jz_identify_x1000_spl(const void* data, size_t len)
118static const struct scramble_model_info { 118static const struct scramble_model_info {
119 const char* name; 119 const char* name;
120 int model_num; 120 int model_num;
121 size_t offset_crc;
122 size_t offset_name;
123 size_t offset_data;
121} scramble_models[] = { 124} scramble_models[] = {
122 {"fiio", 114}, 125 {"fiio", 114, 0, 4, 8},
123 {NULL, 0}, 126 {"shq1", 115, 0, 4, 8},
127 {"eros", 116, 0, 4, 8},
128 {NULL, 0, 0, 0, 0},
124}; 129};
125 130
126/** \brief Identify a file as a Rockbox `scramble` image 131/** \brief Identify a file as a Rockbox `scramble` image
127 * \param data File data buffer 132 * \param data File data buffer
128 * \param len Length of file 133 * \param len Length of file
129 * \return JZ_SUCCESS if file looks correct, or one of the following errors 134 * \return JZ_SUCCESS if file looks correct, or one of the following errors
130 * \retval JZ_IDERR_WRONG_SIZE file too small to be valid
131 * \retval JZ_IDERR_UNRECOGNIZED_MODEL unsupported/unknown model type 135 * \retval JZ_IDERR_UNRECOGNIZED_MODEL unsupported/unknown model type
132 * \retval JZ_IDERR_BAD_CHECKSUM checksum mismatch 136 * \retval JZ_IDERR_BAD_CHECKSUM checksum mismatch
133 */ 137 */
134int jz_identify_scramble_image(const void* data, size_t len) 138int jz_identify_scramble_image(const void* data, size_t len)
135{ 139{
136 /* 4 bytes checksum + 4 bytes player model */ 140 const uint8_t* dat;
137 if(len < 8) 141 const struct scramble_model_info* model_info;
138 return JZ_IDERR_WRONG_SIZE; 142 uint32_t sum, file_sum;
143
144 dat = (const uint8_t*)data;
145 model_info = &scramble_models[0];
139 146
140 /* Look up the model number */ 147 /* Look up the model number */
141 const uint8_t* dat = (const uint8_t*)data; 148 for(; model_info->name != NULL; ++model_info) {
142 const struct scramble_model_info* model_info = &scramble_models[0]; 149 if(model_info->offset_name + 4 > len)
143 for(; model_info->name != NULL; ++model_info) 150 continue;
144 if(!memcmp(&dat[4], model_info->name, 4)) 151 if(!memcmp(&dat[model_info->offset_name], model_info->name, 4))
145 break; 152 break;
153 }
146 154
147 if(model_info->name == NULL) 155 if(model_info->name == NULL)
148 return JZ_IDERR_UNRECOGNIZED_MODEL; 156 return JZ_IDERR_UNRECOGNIZED_MODEL;
149 157
150 /* Compute the checksum */ 158 /* Compute the checksum */
151 uint32_t sum = model_info->model_num; 159 sum = model_info->model_num;
152 for(size_t i = 8; i < len; ++i) 160 for(size_t i = model_info->offset_data; i < len; ++i)
153 sum += dat[i]; 161 sum += dat[i];
154 162
155 /* Compare with file's checksum, it's stored in big-endian form */ 163 /* Compare with file's checksum, it's stored in big-endian form */
156 uint32_t fsum = (dat[0] << 24) | (dat[1] << 16) | (dat[2] << 8) | dat[3]; 164 dat += model_info->offset_crc;
157 if(sum != fsum) 165 file_sum = (dat[0] << 24) | (dat[1] << 16) | (dat[2] << 8) | dat[3];
166 if(sum != file_sum)
158 return JZ_IDERR_BAD_CHECKSUM; 167 return JZ_IDERR_BAD_CHECKSUM;
159 168
160 return JZ_SUCCESS; 169 return JZ_SUCCESS;