root/platform/sx610hs/sub/100a/filewrite.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. log_fwt_msg
  2. log_fwt_start
  3. filewritetask
  4. sub_FF3970B0_my
  5. sub_FF397958_my
  6. sub_FF397288_my

   1 /*
   2  * filewrite.c - auto-generated by CHDK code_gen.
   3  */
   4 #include "lolevel.h"
   5 #include "platform.h"
   6 
   7 #ifdef CAM_HAS_FILEWRITETASK_HOOK
   8 // debug
   9 #define FILEWRITE_DEBUG_LOG 1
  10 extern void _LogCameraEvent(int id,const char *fmt,...);
  11 
  12 typedef struct {
  13     unsigned int address;
  14     unsigned int length;
  15 } cam_ptp_data_chunk; //camera specific structure
  16 
  17 #define MAX_CHUNKS_FOR_FWT 7 // filewritetask is prepared for this many chunks
  18 
  19 
  20 // comments below from elph180, firmware code appears very similar, offsets verified
  21 /*
  22  * fwt_data_struct: defined here as it's camera dependent
  23  * unneeded members are designated with unkn
  24  * file_offset, full_size, seek_flag only needs to be defined for cameras with CAM_FILEWRITETASK_SEEK/CAM_FILEWRITETASK_MULTIPASS
  25  * pdc is always required
  26  * name is not currently used
  27  */
  28 typedef struct
  29 {
  30     int unkn1; // message number
  31     int file_offset;
  32     int maybe_full_size; // maybe, on g7x wasn't always full size
  33     int unkn2, unkn3;
  34     int unkn4;
  35     cam_ptp_data_chunk pdc[MAX_CHUNKS_FOR_FWT];
  36     int maybe_seek_flag;      // 0x2083 jpeg, 0x100 create dir, 0x200 CacheFlush
  37     int unkn5, unkn6;
  38     char name[32];      // offset from start 0x5c, from Open case
  39 } fwt_data_struct;
  40 /*
  41 observed message sequences
  42 JPEG
  43 12 maybe_full_size = 0x004437ac (=actual size), seek_flag = 0x2083
  44  0 chunk 0-0x004437ac
  45  1 chunk 0-0
  46  7 close
  47 
  48 new directory
  49 11 seek_flag = 0x100
  50 before m 12 for shot, after raw hook in capt_seq
  51 
  52 after shot
  53 10 A/ seek_flag 0x200
  54 */
  55 
  56 #include "../../../generic/filewrite.c"
  57 
  58 #ifdef FILEWRITE_DEBUG_LOG
  59 void log_fwt_msg(fwt_data_struct *fwd)
  60 {
  61     int m=fwd->unkn1;
  62     _LogCameraEvent(0x60,"fw m:%d o:0x%08x fs:0x%08x sf:0x%x",m,fwd->file_offset,fwd->maybe_full_size,fwd->maybe_seek_flag);
  63     _LogCameraEvent(0x60,"fw %s",fwd->name);
  64     if(m >= 0 && m <=6) {
  65         _LogCameraEvent(0x60,"fw chunk adr:0x%08x l:0x%08x",fwd->pdc[m].address,fwd->pdc[m].length);
  66     }
  67     _LogCameraEvent(0x60,"fw u %08x %08x %08x %08x %08x %08x",fwd->unkn2,fwd->unkn3,fwd->unkn4,fwd->unkn5,fwd->unkn6);
  68 }
  69 
  70 void log_fwt_start(void)
  71 {
  72     _LogCameraEvent(0x60,"fw start");
  73 }
  74 #endif
  75 
  76 
  77 /*************************************************************/
  78 //** filewritetask @ 0xFF397488 - 0xFF3975C8, length=81
  79 void __attribute__((naked,noinline)) filewritetask() {
  80 asm volatile (
  81 "    STMFD   SP!, {R1-R5,LR} \n"
  82 #ifdef FILEWRITE_DEBUG_LOG
  83 "bl log_fwt_start\n"
  84 #endif
  85 "    LDR     R5, =0xF2C4 \n"
  86 
  87 "loc_FF397490:\n"
  88 "    MOV     R2, #0 \n"
  89 "    LDR     R0, [R5, #0x14] \n"
  90 "    ADD     R1, SP, #8 \n"
  91 "    BL      sub_006B8490 /*_ReceiveMessageQueue*/ \n"
  92 "    CMP     R0, #0 \n"
  93 "    LDRNE   R2, =0x477 \n"
  94 "    LDRNE   R1, =0xFF3975FC /*'dwFWrite.c'*/ \n"
  95 "    MOVNE   R0, #0 \n"
  96 "    BLNE    _DebugAssert \n"
  97 #ifdef FILEWRITE_DEBUG_LOG
  98 "ldr     r0, [sp,#8]\n"
  99 "bl log_fwt_msg\n"
 100 #endif
 101 "    LDR     R0, [SP, #8] \n"
 102 "    LDR     R1, [R0] \n"
 103 "    CMP     R1, #0xD \n"
 104 "    ADDCC   PC, PC, R1, LSL#2 \n"
 105 "    B       loc_FF397490 \n"
 106 "    B       loc_FF3975BC \n"
 107 "    B       loc_FF3975BC \n"
 108 "    B       loc_FF3975BC \n"
 109 "    B       loc_FF3975BC \n"
 110 "    B       loc_FF3975BC \n"
 111 "    B       loc_FF3975BC \n"
 112 "    B       loc_FF3975BC \n"
 113 "    B       loc_FF3975C4 \n"
 114 "    B       loc_FF3974FC \n"
 115 "    B       loc_FF397584 \n"
 116 "    B       loc_FF3975B4 \n"
 117 "    B       loc_FF397574 \n"
 118 "    B       loc_FF39757C \n"
 119 
 120 "loc_FF3974FC:\n"
 121 "    MOV     R0, #0 \n"
 122 "    STR     R0, [SP] \n"
 123 
 124 "loc_FF397504:\n"
 125 "    LDR     R0, [R5, #0x14] \n"
 126 "    MOV     R1, SP \n"
 127 "    BL      sub_006B870C /*_GetNumberOfPostedMessages*/ \n"
 128 "    LDR     R0, [SP] \n"
 129 "    CMP     R0, #0 \n"
 130 "    BEQ     loc_FF397530 \n"
 131 "    LDR     R0, [R5, #0x14] \n"
 132 "    MOV     R2, #0 \n"
 133 "    ADD     R1, SP, #4 \n"
 134 "    BL      sub_006B8490 /*_ReceiveMessageQueue*/ \n"
 135 "    B       loc_FF397504 \n"
 136 
 137 "loc_FF397530:\n"
 138 "    LDR     R0, [R5, #8] \n"
 139 "    MOV     R4, R5 \n"
 140 "    CMN     R0, #1 \n"
 141 "    BEQ     loc_FF397568 \n"
 142 "    BL      _Close \n"
 143 "    MVN     R0, #0 \n"
 144 "    STR     R0, [R4, #8] \n"
 145 "    MOV     R1, #0 \n"
 146 "    MOV     R0, #0x48 \n"
 147 "    BL      sub_FF0C61D0 \n"
 148 "    LDR     R0, =0x18E4E4 \n"
 149 "    BL      sub_FF030C9C \n"
 150 "    MOV     R1, #0 \n"
 151 "    BL      sub_FF02EB58 \n"
 152 
 153 "loc_FF397568:\n"
 154 "    LDR     R0, [R4, #0x10] \n"
 155 "    BL      _GiveSemaphore \n"
 156 "    B       loc_FF397490 \n"
 157 
 158 "loc_FF397574:\n"
 159 "    BL      sub_FF39781C \n"
 160 "    B       loc_FF397490 \n"
 161 
 162 "loc_FF39757C:\n"
 163 "    BL      sub_FF3970B0_my \n"  // --> Patched. Old value = 0xFF3970B0. msg 12 - open, main hook
 164 "    B       loc_FF397490 \n"
 165 
 166 "loc_FF397584:\n"
 167 "    LDR     R1, [R0, #4] \n"
 168 "    MOV     R4, R0 \n"
 169 "    LDR     R0, [R5, #8] \n"
 170 "    MOV     R2, #0 \n"
 171 "    BL      _lseek \n"
 172 "    CMN     R0, #1 \n"
 173 "    LDREQ   R0, =0x9200013 \n"
 174 "    MOVEQ   R1, R4 \n"
 175 "    STREQ   R0, [R4, #0x14] \n"
 176 "    MOVEQ   R0, #7 \n"
 177 "    BLEQ    sub_FF396FE8 \n"
 178 "    B       loc_FF397490 \n"
 179 
 180 "loc_FF3975B4:\n"
 181 "    BL      sub_FF3978BC \n"
 182 "    B       loc_FF397490 \n"
 183 
 184 "loc_FF3975BC:\n"
 185 "    BL      sub_FF397958_my \n"  // --> Patched. Old value = 0xFF397958. msg 0-6 - write chunk
 186 "    B       loc_FF397490 \n"
 187 
 188 "loc_FF3975C4:\n"
 189 "    BL      sub_FF397288_my \n"  // --> Patched. Old value = 0xFF397288. msg 0-7 - close
 190 "    B       loc_FF397490 \n"
 191 );
 192 }
 193 
 194 /*************************************************************/
 195 //** sub_FF3970B0_my @ 0xFF3970B0 - 0xFF397284, length=118
 196 void __attribute__((naked,noinline)) sub_FF3970B0_my() {
 197 asm volatile (
 198 "    STMFD   SP!, {R4-R9,LR} \n"
 199 "    MOV     R4, R0 \n"
 200 //hook placed here to avoid conditional branch a few instructions below (watch out for registers!)
 201 //"  MOV   R0, R4\n"      //data block start, commented out as R0 is already holding what we need
 202 "    BL filewrite_main_hook\n"
 203 "    MOV     R0, R4\n"      //restore register(s)
 204 "    LDR     R0, [R0, #0x50] \n"
 205 "    LDR     R7, =0xF2C4 \n"
 206 "    TST     R0, #5 \n"
 207 "    SUB     SP, SP, #0x3C \n"
 208 "    BEQ     loc_FF3970F4 \n"
 209 "    LDR     R0, [R7, #0x1C] \n"
 210 "    CMP     R0, #0 \n"
 211 "    BLXNE   R0 \n"
 212 "    ADD     R0, R4, #0x5C \n"
 213 "    BL      sub_FF030C9C \n"
 214 "    MOV     R1, #0 \n"
 215 "    BL      sub_FF02EA18 \n"
 216 "    MOV     R1, #0 \n"
 217 "    MOV     R0, #0x47 \n"
 218 "    BL      sub_FF0C61D0 \n"
 219 
 220 "loc_FF3970F4:\n"
 221 "    LDR     R0, [R4, #0x50] \n"
 222 "    TST     R0, #1 \n"
 223 "    BEQ     loc_FF397258 \n"
 224 "    LDR     R0, [R4, #0x10] \n"
 225 "    BL      sub_FF02D8BC \n"
 226 "    LDR     R0, [R4, #0x50] \n"
 227 "    LDR     R5, =0x301 \n"
 228 "    TST     R0, #0x10 \n"
 229 "    MOVNE   R5, #9 \n"
 230 "    BNE     loc_FF397124 \n"
 231 "    TST     R0, #0x40 \n"
 232 "    MOVNE   R5, #1 \n"
 233 
 234 "loc_FF397124:\n"
 235 "    TST     R0, #0x20 \n"
 236 "    BNE     loc_FF397138 \n"
 237 "    LDR     R0, [R4, #0x58] \n"
 238 "    CMP     R0, #1 \n"
 239 "    BNE     loc_FF39713C \n"
 240 
 241 "loc_FF397138:\n"
 242 "    ORR     R5, R5, #0x8000 \n"
 243 
 244 "loc_FF39713C:\n"
 245 "    LDR     R9, =0x1B6 \n"
 246 "    ADD     R8, R4, #0x5C \n"
 247 "    LDR     R6, [R4, #0x10] \n"
 248 "    MOV     R2, R9 \n"
 249 "    MOV     R1, R5 \n"
 250 "    MOV     R0, R8 \n"
 251 "    BL      fwt_open \n"  // --> Patched. Old value = _Open.
 252 "    CMN     R0, #1 \n"
 253 "    BNE     loc_FF3971BC \n"
 254 "    MOV     R0, R8 \n"
 255 "    BL      sub_FF027684 \n"
 256 "    MOV     R2, #0xF \n"
 257 "    MOV     R1, R8 \n"
 258 "    ADD     R0, SP, #4 \n"
 259 "    BL      sub_006DAA10 \n"
 260 "    MOV     R0, #0 \n"
 261 "    LDR     R1, =0x41FF \n"
 262 "    STRB    R0, [SP, #0x13] \n"
 263 "    STR     R1, [SP, #0x24] \n"
 264 "    MOV     R1, #0x10 \n"
 265 "    STR     R0, [SP, #0x2C] \n"
 266 "    STR     R1, [SP, #0x28] \n"
 267 "    ADD     R1, SP, #0x24 \n"
 268 "    ADD     R0, SP, #4 \n"
 269 "    STR     R6, [SP, #0x30] \n"
 270 "    STR     R6, [SP, #0x34] \n"
 271 "    STR     R6, [SP, #0x38] \n"
 272 "    BL      sub_FF02E37C \n"
 273 "    MOV     R2, R9 \n"
 274 "    MOV     R1, R5 \n"
 275 "    MOV     R0, R8 \n"
 276 "    BL      _Open \n"
 277 
 278 "loc_FF3971BC:\n"
 279 "    CMN     R0, #1 \n"
 280 "    MOV     R5, R0 \n"
 281 "    STR     R0, [R7, #8] \n"
 282 "    BNE     loc_FF397218 \n"
 283 "    MOV     R1, #0 \n"
 284 "    MOV     R0, #0x48 \n"
 285 "    BL      sub_FF0C61D0 \n"
 286 "    ADD     R0, R4, #0x5C \n"
 287 "    BL      sub_FF030C9C \n"
 288 "    LDR     R1, [R7, #0x20] \n"
 289 "    BL      sub_FF02EB58 \n"
 290 "    LDR     R0, [R7, #0x18] \n"
 291 "    CMP     R0, #0 \n"
 292 "    BEQ     loc_FF397280 \n"
 293 "    LDR     R5, =0x9200001 \n"
 294 "    MOV     R0, R4 \n"
 295 "    MOV     R1, R5 \n"
 296 "    BL      sub_FF396F84 \n"
 297 "    LDR     R1, [R7, #0x18] \n"
 298 "    MOV     R0, R5 \n"
 299 "    ADD     SP, SP, #0x3C \n"
 300 "    LDMFD   SP!, {R4-R9,LR} \n"
 301 "    BX      R1 \n"
 302 
 303 "loc_FF397218:\n"
 304 "    LDR     R0, =0x18E4E4 \n"
 305 "    MOV     R2, #0x20 \n"
 306 "    ADD     R1, R4, #0x5C \n"
 307 "    BL      sub_006DABF8 \n"
 308 "    LDR R3, =current_write_ignored\n"
 309 "    LDR R3, [R3]\n"
 310 "    CMP R3, #0\n"
 311 "    BNE loc_C\n" // jump over the next block
 312 "    LDR     R0, [R4, #0x50] \n"
 313 "    TST     R0, #0x80 \n"
 314 "    BEQ     loc_FF397258 \n"
 315 "    LDR     R1, [R4, #0xC] \n"
 316 "    MOV     R0, R5 \n"
 317 "    BL      sub_FF027354 \n"
 318 "    CMP     R0, #0 \n"
 319 "    LDREQ   R0, =0x9200017 \n"
 320 "    MOVEQ   R1, R4 \n"
 321 "    STREQ   R0, [R4, #0x14] \n"
 322 "    MOVEQ   R0, #7 \n"
 323 "    BEQ     loc_FF39727C \n"
 324 
 325 "loc_FF397258:\n"
 326 "loc_C:\n"
 327 "    LDR     R0, [R4, #0x50] \n"
 328 "    TST     R0, #0x40 \n"
 329 "    LDREQ   R0, [R4, #4] \n"
 330 "    CMPEQ   R0, #0 \n"
 331 "    MOVNE   R1, R4 \n"
 332 "    MOVNE   R0, #9 \n"
 333 "    BLNE    sub_FF396FE8 \n"
 334 "    MOV     R1, R4 \n"
 335 "    MOV     R0, #0 \n"
 336 
 337 "loc_FF39727C:\n"
 338 "    BL      sub_FF396FE8 \n"
 339 
 340 "loc_FF397280:\n"
 341 "    ADD     SP, SP, #0x3C \n"
 342 "    LDMFD   SP!, {R4-R9,PC} \n"
 343 );
 344 }
 345 
 346 /*************************************************************/
 347 //** sub_FF397958_my @ 0xFF397958 - 0xFF397A3C, length=58
 348 void __attribute__((naked,noinline)) sub_FF397958_my() {
 349 asm volatile (
 350 "    STMFD   SP!, {R4-R10,LR} \n"
 351 "    MOV     R5, R0 \n"
 352 "    LDR     R0, [R0] \n"
 353 "    CMP     R0, #6 \n"
 354 "    BHI     loc_FF397984 \n"
 355 "    ADD     R0, R5, R0, LSL#3 \n"
 356 "    LDR     R8, [R0, #0x18]! \n"
 357 "    LDR     R7, [R0, #4] \n"
 358 "    CMP     R7, #0 \n"
 359 "    BNE     loc_FF3979A0 \n"
 360 "    B       loc_FF397994 \n"
 361 
 362 "loc_FF397984:\n"
 363 "    MOV     R2, #0x3B4 \n"
 364 "    LDR     R1, =0xFF3975FC /*'dwFWrite.c'*/ \n"
 365 "    MOV     R0, #0 \n"
 366 "    BL      _DebugAssert \n"
 367 
 368 "loc_FF397994:\n"
 369 "    MOV     R1, R5 \n"
 370 "    MOV     R0, #7 \n"
 371 "    B       loc_FF397A38 \n"
 372 
 373 "loc_FF3979A0:\n"
 374 "    LDR     R9, =0xF2C4 \n"
 375 "    MOV     R4, R7 \n"
 376 
 377 "loc_FF3979A8:\n"
 378 "    LDR     R0, [R5, #4] \n"
 379 "    CMP     R4, #0x1000000 \n"
 380 "    MOVLS   R6, R4 \n"
 381 "    MOVHI   R6, #0x1000000 \n"
 382 "    BIC     R1, R0, #0xFF000000 \n"
 383 "    CMP     R1, #0 \n"
 384 "    BICNE   R0, R0, #0xFF000000 \n"
 385 "    RSBNE   R0, R0, #0x1000000 \n"
 386 "    CMPNE   R6, R0 \n"
 387 "    MOVHI   R6, R0 \n"
 388 "    LDR     R0, [R9, #8] \n"
 389 "    MOV     R2, R6 \n"
 390 "    MOV     R1, R8 \n"
 391 "    BL      fwt_write \n"  // --> Patched. Old value = _Write.
 392 "    LDR     R1, [R5, #4] \n"
 393 "    CMP     R6, R0 \n"
 394 "    ADD     R1, R1, R0 \n"
 395 "    STR     R1, [R5, #4] \n"
 396 "    BEQ     loc_FF397A08 \n"
 397 "    CMN     R0, #1 \n"
 398 "    LDRNE   R0, =0x9200015 \n"
 399 "    LDREQ   R0, =0x9200005 \n"
 400 "    STR     R0, [R5, #0x14] \n"
 401 "    B       loc_FF397994 \n"
 402 
 403 "loc_FF397A08:\n"
 404 "    SUB     R4, R4, R0 \n"
 405 "    CMP     R4, R7 \n"
 406 "    LDRCS   R2, =0x3DE \n"
 407 "    LDRCS   R1, =0xFF3975FC /*'dwFWrite.c'*/ \n"
 408 "    ADD     R8, R8, R0 \n"
 409 "    MOVCS   R0, #0 \n"
 410 "    BLCS    _DebugAssert \n"
 411 "    CMP     R4, #0 \n"
 412 "    BNE     loc_FF3979A8 \n"
 413 "    LDR     R0, [R5] \n"
 414 "    MOV     R1, R5 \n"
 415 "    ADD     R0, R0, #1 \n"
 416 
 417 "loc_FF397A38:\n"
 418 "    LDMFD   SP!, {R4-R10,LR} \n"
 419 "    B       sub_FF396FE8 \n"
 420 );
 421 }
 422 
 423 /*************************************************************/
 424 //** sub_FF397288_my @ 0xFF397288 - 0xFF39731C, length=38
 425 void __attribute__((naked,noinline)) sub_FF397288_my() {
 426 asm volatile (
 427 "    STMFD   SP!, {R4-R6,LR} \n"
 428 "    MOV     R4, R0 \n"
 429 "    LDR     R0, [R0, #0x50] \n"
 430 "    LDR     R5, =0xF2C4 \n"
 431 "    TST     R0, #0xA \n"
 432 "    SUB     SP, SP, #0x38 \n"
 433 "    BEQ     sub_FF397454 \n"
 434 "    TST     R0, #2 \n"
 435 "    BEQ     sub_FF397330 \n"
 436 "    TST     R0, #0x80 \n"
 437 "    LDRNE   R0, [R4, #0xC] \n"
 438 "    LDRNE   R1, [R4, #8] \n"
 439 "    CMPNE   R1, R0 \n"
 440 "    BEQ     loc_FF3972F8 \n"
 441 "    LDR     R0, [R5, #8] \n"
 442 "    BL      sub_FF027354 \n"
 443 "    CMP     R0, #0 \n"
 444 "    LDREQ   R0, =0x9200017 \n"
 445 "    STREQ   R0, [R4, #0x14] \n"
 446 "    BEQ     loc_FF3972F8 \n"
 447 "    LDR     R0, [R4, #8] \n"
 448 "    ADD     R1, SP, #0x20 \n"
 449 "    STR     R0, [R4, #4] \n"
 450 "    ADD     R0, R4, #0x5C \n"
 451 "    BL      sub_FF02E2C4 \n"
 452 "    CMP     R0, #1 \n"
 453 "    LDREQ   R0, [R4, #4] \n"
 454 "    STREQ   R0, [SP, #0x28] \n"
 455 
 456 "loc_FF3972F8:\n"
 457 "    LDR     R0, [R5, #8] \n"
 458 "    CMN     R0, #1 \n"
 459 "    BEQ     sub_FF397330 \n"
 460 "    LDR     R1, [R4, #0x58] \n"
 461 "    LDR     R6, =0x9200003 \n"
 462 "    CMP     R1, #1 \n"
 463 "    BNE     loc_FF39731C \n"
 464 "    LDR R3, =current_write_ignored\n"
 465 "    LDR R3, [R3]\n"
 466 "    CMP R3, #0\n"
 467 "    BNE loc_D\n" // jump over the next block
 468 "    BL      sub_FF0270A8 \n"
 469 "    B       sub_FF397320 \n"
 470 
 471 "loc_FF39731C:\n"
 472 "loc_D:\n"
 473 "    BL      fwt_close \n"  // --> Patched. Old value = _Close.
 474 "    LDR     PC, =0xFF397320 \n"  // Continue in firmware
 475 );
 476 }
 477 #endif // CAM_HAS_FILEWRITETASK_HOOK

/* [<][>][^][v][top][bottom][index][help] */