-
Notifications
You must be signed in to change notification settings - Fork 1
/
rkimage.cpp
2299 lines (1982 loc) · 60.2 KB
/
rkimage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2011 Rockchip Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define _FILE_OFFSET_BITS 64
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fs_mgr.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <sys/wait.h>
#include "cutils/properties.h"
#include "common.h"
#include "install.h"
#include "mincrypt/rsa.h"
#include "minui/minui.h"
#include "minzip/SysUtil.h"
#include "minzip/Zip.h"
#include "minzip/DirUtil.h"
#include "mtdutils/mounts.h"
#include "mtdutils/mtdutils.h"
//#include "mincrypt/sha.h"
#include "ui.h"
#include "screen_ui.h"
#include "roots.h"
#include "bootloader.h"
#include "rkimage.h"
#include "mtdutils/rk29.h"
#include "cutils/android_reboot.h"
//extern "C" {
#include "edify/expr.h"
#include "applypatch/applypatch.h"
//#include "verifier.h"
//}
#ifdef USE_RADICAL_UPDATE
#include "radical_update.h"
#endif
#include "./rkupdate/Upgrade.h"
extern bool gIfBoardIdCustom;
extern bool bIfUpdateLoader;
extern RecoveryUI* ui;
extern struct selabel_handle *sehandle;
//extern "C" int yyparse(Expr** root, int* error_count);
//extern "C" void* yy_scan_string (const char *yy_str );
extern int yyparse(Expr** root, int* error_count);
extern void* yy_scan_string (const char *yy_str );
//The origin is the partition, for example recover from backup
static int read_partition(int fd, off_t offset, char* data, int size);
// The origin is the file, for example recover from udisk or sdcard
static int read_file(int fd, off_t offset, char* data, int size);
#define STEP_SIZE 1024*1024
#define MY_READ(fd, offset, data, size)\
(g_src_isFile?\
read_file(fd, offset, data, size):\
read_partition(fd, offset, data, size) )
//the update package path
char g_package_target[128] = {0}; // /sdcard/update.img
char g_package_root_path[128] = {0};
bool g_src_isFile = false;
RKIMAGE_HDR g_imagehdr;
unsigned int gFwOffset = 0;
extern int dirCreateHierarchy(const char *path, int mode,
const struct utimbuf *timestamp, bool stripFileName,
struct selabel_handle *sehnd);
extern int property_get(const char *key, char *value, const char *default_value);
extern FILE* fopen_root_path(const char *root_path, const char *mode);
extern int wipe_data(int wipe_flags);
//===================== check usbboot mode ==========================
int check_usbboot()
{
char param[1024];
int fd, ret;
char *s=NULL;
memset(param,0,1024);
fd= open("/proc/cmdline", O_RDONLY);
ret = read(fd, (char*)param, 1024);
printf("cmdline=%s\n",param);
s = strstr(param,"usbfwupdate");
if(s!= NULL)
return 0;
else
return -1;
}
//===================== check sdboot mode ==========================
int check_sdboot(void)
{
char param[1024];
int fd, ret;
char *s=NULL;
printf("read cmdline\n");
memset(param,0,1024);
fd= open("/proc/cmdline", O_RDONLY);
ret = read(fd, (char*)param, 1024);
s = strstr(param,"sdfwupdate");
if(s!= NULL)
return 0;
else
return -1;
}
//====================update rkimage loader ==========================
static void dump_data(const char *data, int len) {
int pos;
for (pos = 0; pos < len; ) {
LOGW("%05x: %02x", pos, data[pos]);
for (++pos; pos < len && (pos % 24) != 0; ++pos) {
LOGW(" %02x", data[pos]);
}
LOGW("\n");
}
}
#define BOOTSIGN "RK28@Copyright2008Rockchip"
#define BOOTSIGN_SIZE 32
#define CHECK_SIZE 16
#define HEADINFO_SIZE 512
#define BCD2INT(num) (((((num)>>4)&0x0F)*10)+((num)&0x0F))
typedef struct _rk_time {
unsigned short usYear;
unsigned short usMonth;
unsigned short usDate;
unsigned short usHour;
unsigned short usMinute;
unsigned short usSecond;
}RK_TIME;
typedef struct _RK28BOOT_HEAD{
char szSign[BOOTSIGN_SIZE];
unsigned char bMD5Check[CHECK_SIZE];
RK_TIME tmCreateTime;
unsigned int uiMajorVersion;
unsigned int uiMinorVersion;
unsigned int uiUsbDataOffset;
unsigned int uiUsbDataLen;
unsigned int uiUsbBootOffset;
unsigned int uiUsbBootLen;
unsigned int uiFlashDataOffset;
unsigned int uiFlashDataLen;
unsigned int uiFlashBootOffset;
unsigned int uiFlashBootLen;
unsigned char ucRc4Flag;
unsigned int MergerVersion; // Generate Boot file Merger tools used the version number of the high 16 bytes (mainly low 16 byte acted as the version number of the version number)
}RK28BOOT_HEAD, *PRK28BOOT_HEAD;
#define BOOT_RESERVED_SIZE 57
typedef enum
{
RK27_DEVICE=1,
RK28_DEVICE=2,
RKNANO_DEVICE=4
}ENUM_RKDEVICE_TYPE;
typedef enum
{
ENTRY471=1,
ENTRY472=2,
ENTRYLOADER=4
}ENUM_RKBOOTENTRY;
#pragma pack(1)
typedef struct
{
unsigned short usYear;
unsigned char ucMonth;
unsigned char ucDay;
unsigned char ucHour;
unsigned char ucMinute;
unsigned char ucSecond;
}STRUCT_RKTIME,*PSTRUCT_RKTIME;
typedef struct
{
unsigned int uiTag;
unsigned short usSize;
unsigned int dwVersion;
unsigned int dwMergeVersion;
STRUCT_RKTIME stReleaseTime;
ENUM_RKDEVICE_TYPE emSupportChip;
unsigned char temp[12];
unsigned char ucLoaderEntryCount;
unsigned int dwLoaderEntryOffset;
unsigned char ucLoaderEntrySize;
unsigned char ucSignFlag;
unsigned char ucRc4Flag;
unsigned char reserved[BOOT_RESERVED_SIZE];
}STRUCT_RKBOOT_HEAD,*PSTRUCT_RKBOOT_HEAD;
typedef struct
{
unsigned char ucSize;
ENUM_RKBOOTENTRY emType;
unsigned char szName[40];
unsigned int dwDataOffset;
unsigned int dwDataSize;
unsigned int dwDataDelay;//in seconds
}STRUCT_RKBOOT_ENTRY,*PSTRUCT_RKBOOT_ENTRY;
#pragma pack()
int make_loader_data(const char* old_loader, char* new_loader, int *new_loader_size)//path, RKIMAGE_HDR *hdr)
{
int i,j;
PSTRUCT_RKBOOT_ENTRY pFlashDataEntry = NULL;
PSTRUCT_RKBOOT_ENTRY pFlashBootEntry = NULL;
STRUCT_RKBOOT_HEAD *boot_hdr = NULL;
RK28BOOT_HEAD *new_hdr = NULL;
boot_hdr = (STRUCT_RKBOOT_HEAD*)old_loader;
// get the data block information of FlashData/FlashBoot
for (i=0;i<boot_hdr->ucLoaderEntryCount;i++)
{
PSTRUCT_RKBOOT_ENTRY pEntry;
pEntry = (PSTRUCT_RKBOOT_ENTRY)(old_loader+boot_hdr->dwLoaderEntryOffset+(boot_hdr->ucLoaderEntrySize*i));
char name[10] = "";
for(j=0; j<20 && pEntry->szName[j]; j+=2)
name[j/2] = pEntry->szName[j];
if( !strcmp( name, "FlashData" ) )
pFlashDataEntry = pEntry;
else if( !strcmp( name, "FlashBoot" ) )
pFlashBootEntry = pEntry;
}
if(pFlashDataEntry == NULL || pFlashBootEntry == NULL)
return -1;
// Construct a new Loader data, to local Loader to upgrade
new_hdr = (RK28BOOT_HEAD*)new_loader;
memset(new_hdr, 0, HEADINFO_SIZE);
strcpy(new_hdr->szSign, BOOTSIGN);
new_hdr->tmCreateTime.usYear = boot_hdr->stReleaseTime.usYear;
new_hdr->tmCreateTime.usMonth= boot_hdr->stReleaseTime.ucMonth;
new_hdr->tmCreateTime.usDate= boot_hdr->stReleaseTime.ucDay;
new_hdr->tmCreateTime.usHour = boot_hdr->stReleaseTime.ucHour;
new_hdr->tmCreateTime.usMinute = boot_hdr->stReleaseTime.ucMinute;
new_hdr->tmCreateTime.usSecond = boot_hdr->stReleaseTime.ucSecond;
new_hdr->uiMajorVersion = (boot_hdr->dwVersion&0x0000FF00)>>8;
new_hdr->uiMajorVersion = BCD2INT(new_hdr->uiMajorVersion);
new_hdr->uiMinorVersion = boot_hdr->dwVersion&0x000000FF;
new_hdr->uiMinorVersion = BCD2INT(new_hdr->uiMinorVersion);
new_hdr->uiFlashDataOffset = HEADINFO_SIZE;
new_hdr->uiFlashDataLen = pFlashDataEntry->dwDataSize;
new_hdr->uiFlashBootOffset = new_hdr->uiFlashDataOffset+new_hdr->uiFlashDataLen;
new_hdr->uiFlashBootLen = pFlashBootEntry->dwDataSize;
new_hdr->ucRc4Flag = boot_hdr->ucRc4Flag;
memcpy(new_loader+new_hdr->uiFlashDataOffset, old_loader+pFlashDataEntry->dwDataOffset, pFlashDataEntry->dwDataSize);
memcpy(new_loader+new_hdr->uiFlashBootOffset, old_loader+pFlashBootEntry->dwDataOffset, pFlashBootEntry->dwDataSize);
*new_loader_size = new_hdr->uiFlashBootOffset+new_hdr->uiFlashBootLen;
// dump_data(new_loader, HEADINFO_SIZE);
return 0;
}
//=======================================================
RKIMAGE_ITEM* FindItem(RKIMAGE_HDR* prkimage, const char* name)
{
int i=0;
for(i=0; i<prkimage->item_count; i++)
if( !strcmp(prkimage->item[i].name, name) )
return prkimage->item+i;
return NULL;
}
extern unsigned long CRC_32_NEW( unsigned char * aData, unsigned int aSize, unsigned int prev_crc );
extern "C" int check_image_rsa(const char* imageFilePath, unsigned int fwOffset, unsigned int fwsize);
/*
success return 0
error return -1
*/
int check_image_crc(const char* mtddevname, unsigned long image_size)
{
int size = 32<<9;
char buffer[16*1024] = "";
unsigned int crc = 0;
int remain = image_size;
int read_count = 0;
int r=0;
int file_offset = gFwOffset;
int fdread = open(mtddevname, O_RDONLY);
if(fdread < 0)
{
LOGE("Can't open part(%s)\n(%s)\n", mtddevname, strerror(errno));
return -1;
}
lseek(fdread, gFwOffset, SEEK_SET);
while(remain > 0)
{
read_count = remain>size?size:remain;
if( 0 != MY_READ(fdread, g_src_isFile?-1:file_offset, buffer, read_count) )
{
LOGE("Can't read (%s)\n(%s)\n", mtddevname, strerror(errno));
close(fdread);
return -1;
}
file_offset += read_count;
crc = CRC_32_NEW((unsigned char*)buffer, read_count, crc);
remain -= read_count;
}
// Read in the end of the file additional CRC32 check code
if( 0 != MY_READ(fdread, g_src_isFile?-1:file_offset, buffer, 4) )
{
LOGE("Can't read (%s)\n(%s)\n", mtddevname, strerror(errno));
close(fdread);
return -1;
}
file_offset += read_count;
close(fdread);
if( crc != *(unsigned int*)buffer )
{
LOGE("Check failed\n");
LOGI("crc = %04lx buffer=%04lx \n", crc, *(unsigned int*)buffer);
return -1;
}
LOGI("Check crc okay.\n");
return 0;
}
void adjustFileOffset(RKIMAGE_HDR* hdr, int offset)
{
int i=0;
for(i=0; i<hdr->item_count; i++)
hdr->item[i].offset += offset;
return;
}
/*
from fd offset position, Continuous read size byte of data store in a data
offset < 0 : Not seek fd operation
success return 0
fail return -1
*/
static int read_file(int fd, off_t offset, char* data, int size)
{
ssize_t r = 0;
if( offset >= 0 )
{
lseek(fd, offset, SEEK_SET);
}
r = read(fd, (char*)data, size);
if (r != size)
{
// LOGE("Read failed: (%s)\n", strerror(errno));
return -1;
}
return 0;
}
/*
from fd offset position, Continuous read size byte of data store in a data
offset < 0 : Not seek fd operation
success return 0
fail return -1
*/
static int read_partition(int fd, off_t offset, char* data, int size)
{
char buf[512];
int m,n;
int remain = size;
if( offset >= 0 )
{
m = offset/512;
n = offset%512;
lseek(fd, m*512, SEEK_SET);
if(n != 0)
{
read(fd, buf, 512);
memcpy(data, buf+n, 512-n);
data += 512-n;
remain -= 512-n;
}
}
while(remain > 0)
{
if( read(fd, buf, 512) != 512 )
{
// LOGE("read error: (%s)\n", strerror(errno));
return -1;
}
if( remain > 512 )
{
memcpy(data, buf, 512);
data += 512;
remain -= 512;
}
else
{
memcpy(data, buf, remain);
data += remain;
remain = 0;
}
// LOGI("remain=%d\n", remain);
}
return 0;
}
void trim(char *strIn, char *strOut){
int i, j;
i = 0;
j = strlen(strIn) - 1;
while(strIn[i] == ' ')
++i;
while(strIn[j] == ' ')
--j;
strncpy(strOut, strIn + i , j - i + 1);
strOut[j - i + 1] = '\0';
}
/*
success return 0
fail return other
*/
static int CheckImageFile(const char* path, RKIMAGE_HDR* hdr)
{
/* Try to open the image.
*/
int fd = open(path, O_RDONLY);
if (fd < 0) {
LOGE("Can't open %s\n", path);
return -1;
}
/* Need to read the documents before 512 bytes,
* to determine whether the new way of packing update.
* If not be, according to the way to handle before then
* If the new packaging mode, the firmware of the offset each file to adjust accordingly
*
*/
gFwOffset = 0;
char buf[512] = "";
unsigned int fwSize = 0;
if( 0 != MY_READ(fd, 0, buf, 512) )
{
LOGE("Can't read %s\n(%s)\n", path, strerror(errno));
close(fd);
return -2;
}
// Confirm whether the new packaging format
if( *((unsigned int*)buf) == 0x57464B52 )
{
gFwOffset = *(unsigned int*)(buf+0x21);
fwSize = *(unsigned int *)(buf + 0x25);
}
if(0 != MY_READ(fd, gFwOffset, (char*)hdr, sizeof(RKIMAGE_HDR)))
{
LOGE("Can't read %s\n(%s)\n", path, strerror(errno));
close(fd);
return -2;
}
close(fd);
if(hdr->tag != RKIMAGE_TAG)
{
LOGI("tag: %x\n", hdr->tag);
LOGE("Invalid image\n");
return -3;
}
/* check product model */
char model[256] = {0};
if( property_get("ro.product.model", model, NULL) <= 0)
{
LOGE("Not found local model!\n");
return -4;
}
trim(model, model);
trim(hdr->machine_model, hdr->machine_model);
LOGI("Local model: %s\nUpdate model: %s\n", model, hdr->machine_model);
if(strcmp(model, hdr->machine_model))
{
LOGE("Not support firmware\n");
return -5;
}
LOGI("Checking...\n");
#ifdef USE_RSA_CHECK
if(check_image_rsa(path, gFwOffset, fwSize))
return -6;
#else
if(check_image_crc(path, hdr->size))
return -6;
#endif
if(gFwOffset)
adjustFileOffset(hdr, gFwOffset);
return 0;
}
int open_file_path(const char *path, int mode) {
if (ensure_path_mounted(path) != 0) {
LOGE("Can't mount %s\n", path);
return -1;
}
// When writing, try to create the containing directory, if necessary.
// Use generous permissions, the system (init.rc) will reset them.
dirCreateHierarchy(path, 0777, NULL, 1, sehandle);
int fd = open(path, mode);
if (fd < 0)
{
LOGE("Can't open %s\n", path);
return -3;
}
return fd;
}
// open_partition_path("BACKUP:", O_RDWR, path)
int open_partition_path(const char *part_name, int mode, char* path) {
if (ensure_path_unmounted(part_name) != 0) {
LOGE("Can't unmount %s\n", part_name);
return -1;
}
char *p;
char mtdname[32];
Volume* v = volume_for_path(part_name);
if(!strcmp(v->fs_type, "mtd")) {
#ifdef USE_OLD_NAND_DRIVER
if(p = strstr(v->blk_device,"/dev/block/mtd/by-name/"))
strcpy(mtdname,p+23);
else
strcpy(mtdname,v->blk_device);
const MtdPartition* partition = mtd_find_partition_by_name(mtdname);
if (partition == NULL) {
LOGE("failed to find \"%s\" partition to mount at \"%s\"\n",
mtdname, v->mount_point);
return -1;
}
sprintf(path, "/dev/mtd/mtd%d", mtd_get_partition_index((MtdPartition*)partition));
#else
if(p = strstr(v->blk_device,"/dev/block/rknand_"))
strcpy(path, v->blk_device);
else
sprintf(path, "/dev/block/rknand_%s", v->blk_device);
#endif
}else {
strcpy(path, v->blk_device);
}
int fd = open(path, mode);
if (fd < 0){
LOGE("Can't open %s\n", path);
return -3;
}
return fd;
}
#define MAX_LOADER_LEN 1024*1024
int write_loader(const char* src, const char* dest, int woffset)
{
RKIMAGE_HDR *hdr = &g_imagehdr;
int fd_src, fd_dest, fd_loader;
bool dest_is_file = false;
char destpath[PATH_MAX];
char old_loader[MAX_LOADER_LEN] = "";
char new_loader[MAX_LOADER_LEN] = "";
char *buff;
int new_loader_size = 0;
char* loader_bin = "/tmp/RKLoader.bin";
LOGI("src=%s dest=%s offset=%d\n", src, dest, woffset);
// get loader data
RKIMAGE_ITEM* pItem = (RKIMAGE_ITEM*)FindItem(hdr, src);
if(pItem == NULL)
{
LOGE("Can't find %s \n", src);
return -2;
}
if(pItem->offset > MAX_LOADER_LEN)
{
LOGE("Loader too large\n");
return -2;
}
fd_src = open(g_package_target, O_RDONLY);
if (fd_src <= 0) {
LOGE("Can't open file: %s\n", g_package_target);
return -3;
}
lseek(fd_src, pItem->offset, SEEK_SET);
if( read(fd_src, old_loader, pItem->size) != pItem->size)
{
close(fd_src);
LOGE("Read failed(%s)\n", strerror(errno));
return -4;
}
fd_loader = creat(loader_bin, 0755);
if (fd_loader < 0){
LOGE("Can't make %s\n", loader_bin);
return -4;
}
if(write(fd_loader, old_loader, pItem->size) != pItem->size){
LOGE("Can't write to %s.\n", loader_bin);
return -4;
}
close(fd_loader);
if(!do_rk_firmware_upgrade(loader_bin)){
LOGE("do_rk_firmware_upgrade failed.");
return -4;
}
close(fd_src);
sync();
/*
// create new loader data
if ( (*(unsigned int*)old_loader)==0x544F4F42 )
{// new Loader
if( make_loader_data(old_loader, new_loader, &new_loader_size) )
{
LOGE("Invalid loader file\n");
return -5;
}
buff = new_loader;
}
else
{// old loader
buff = old_loader;
new_loader_size = pItem->size;
}
if( !strcmp(dest, "/backup") )
dest_is_file = false;
LOGI("dest isFile: %d\n", dest_is_file);
if(dest_is_file)
fd_dest = open_file_path(dest, O_RDWR|O_CREAT);
else
fd_dest = open_partition_path(dest, O_RDWR,destpath);
if (fd_dest < 0) {
LOGE("Bad dest path %s\n", dest);
return -6;
}
lseek(fd_dest, woffset, SEEK_SET);
if( write(fd_dest, buff, new_loader_size) != new_loader_size )
{
close(fd_dest);
LOGE("Write failed(%s)\n", strerror(errno));
return -7;
}
sync();
// Read the writing of data and check
lseek(fd_dest, woffset, SEEK_SET);
if( read(fd_dest, old_loader, new_loader_size) != new_loader_size )
{
close(fd_dest);
LOGE("Read failed(%s)\n", strerror(errno));
return -8;
}
close(fd_dest);
if( memcmp(old_loader, buff, new_loader_size) )
{
LOGE("Check failed!\n");
return -9;
}
*/
return 0;
}
int write_image_from_file(const char* src, const char* dest, int woffset)
{
bool dest_is_file = false;
char destpath[PATH_MAX];
int fd_src, fd_dest;
off64_t dest_offset, image_length;
char data_buf[STEP_SIZE] = {0};
off64_t src_remain, dest_remain;
int src_step, dest_step;
int count = 1;
off64_t src_file_offset = 0;
LOGI("src=%s dest=%s offset=%d\n", src, dest, woffset);
fd_src = open(src, O_RDONLY);
if (fd_src == 0) {
LOGE("Can't open file: %s\n", g_package_target);
return -5;
}
image_length = lseek64(fd_src, 0, SEEK_END);
src_remain = image_length;
src_step = STEP_SIZE;
src_file_offset = 0;
LOGI("size of off_t %d\n", sizeof(off64_t));
LOGI("img length is %llu\n", src_remain);
dest_offset = (off64_t)woffset;
// The target is parameter zoning, fixed read write 32 sector
dest_remain = image_length;
dest_step = STEP_SIZE;
if(dest_is_file)
fd_dest = open_file_path(dest, O_RDWR|O_CREAT);
else
fd_dest = open_partition_path(dest, O_RDWR,destpath);
if (fd_dest < 0) {
close(fd_src);
LOGE("Bad dest path %s\n", dest);
return -6;
}
lseek64(fd_dest, dest_offset, SEEK_SET);
// From source data read and write goal
off64_t read_count, write_count, real_count;
bool bWrited = false;
// LOGI("pItem->name=%s\n", pItem->name);
while(src_remain>0 && dest_remain > 0)
{
memset(data_buf, 0, STEP_SIZE);
// read data
read_count = src_remain>src_step?src_step:src_remain;
real_count = pread64(fd_src, data_buf, read_count, src_file_offset);
printf("real_count= %llu ; read_count= %llu \n", real_count, read_count);
if(real_count != read_count)
{
close(fd_src);
close(fd_dest);
LOGE("Read failed(%s)\n", strerror(errno));
return bWrited?-1:-2;
}
src_remain -= read_count;
src_file_offset += read_count;
write_count = (src_remain == 0)?dest_remain:dest_step;
bWrited=true;
if(write(fd_dest, data_buf, dest_is_file?write_count:dest_step) != (dest_is_file?write_count:dest_step) ) {
close(fd_src);
close(fd_dest);
LOGE("Write failed(%s)\n", strerror(errno));
return -1;
}
dest_remain -= write_count;
// LOGI("src: read=%d remain=%d dest: write=%d remain=%d\n", read_count, src_remain, write_count, dest_remain);
}
close(fd_src);
close(fd_dest);
return 0;
}
// PACKAGE:system, "SYSTEM:", offset
// PACKAGE:system, "USERDATA:aaa.img", 0
// PACKAGE == SDCARD:update.img OK
// PACKAGE == BACKUP: OK
/*
* success 0
* read or write fail -1
* read fail before write -2
* others <-2
*/
int write_image(const char* src, const char* dest, int woffset)
{
RKIMAGE_HDR *hdr = &g_imagehdr;
bool dest_is_parameter = false;
bool dest_is_file = false;
char destpath[PATH_MAX];
int fd_src, fd_dest;
int src_offset, dest_offset;
char data_buf[16*1024] = {0};
int src_remain, dest_remain;
int src_step, dest_step;
int count = 1;
int src_file_offset = 0;
LOGI("src=%s dest=%s offset=%d\n", src, dest, woffset);
// Source for the necessary information
RKIMAGE_ITEM* pItem = (RKIMAGE_ITEM*)FindItem(hdr, src);
if(pItem == NULL)
{
LOGE("Can't find %s \n", src);
return -4;
}
fd_src = open(g_package_target, O_RDONLY);
if (fd_src == 0) {
LOGE("Can't open file: %s\n", g_package_target);
return -5;
}
src_offset = pItem->offset;
src_remain = pItem->size;
src_step = 16*1024;
lseek(fd_src, src_offset, SEEK_SET);
src_file_offset = src_offset;
// Get target necessary information
if( !strcmp(dest, "/backup") )
dest_is_file = false;
LOGI("dest isFile: %d\n", dest_is_file);
if( !strcmp(dest, "/parameter") )
{
dest_is_parameter = true;
}
dest_offset = woffset;
// The target is parameter zoning, fixed read write 32 sector
dest_remain = dest_is_parameter?16*1024:pItem->size;
dest_step = 16*1024;
if(dest_is_file)
fd_dest = open_file_path(dest, O_RDWR|O_CREAT);
else
fd_dest = open_partition_path(dest, O_RDWR,destpath);
if (fd_dest < 0) {
close(fd_src);
LOGE("Bad dest path %s\n", dest);
return -6;
}
lseek(fd_dest, dest_offset, SEEK_SET);
// From source data read and write goal
int read_count, write_count;
bool bWrited = false;
bool bSystemEncrypted = false;
int remain_encrypt = 128*1024;
long long sys_key = 0x8399190660919938;
unsigned char plain[16*1024]={0};
bool bFirst = true;
// LOGI("pItem->name=%s\n", pItem->name);
while(src_remain>0 && dest_remain > 0)
{
memset(data_buf, 0, 16*1024);
// read data
read_count = src_remain>src_step?src_step:src_remain;
// if( read(fd_src, data_buf, read_count) != read_count)
if( MY_READ(fd_src, src_file_offset, data_buf, read_count) != 0)
{
close(fd_src);
close(fd_dest);
LOGE("Read failed(%s)\n", strerror(errno));
return bWrited?-1:-2;
}
src_remain -= read_count;
src_file_offset += read_count;
#if 0
if( !strcmp(pItem->name, "system") )// write system
{
if( bFirst && (*(unsigned int*)data_buf)!=0x28cd3d45 )
{// system.img encrypted
// LOGI("encrypt!!!\n");
bSystemEncrypted = true;
}
if( bSystemEncrypted && remain_encrypt>0 )
{
// To decrypt data
// LOGI("remain_decrypt=%d\n", remain_encrypt);
DES_decrypt(data_buf, plain, 16*1024, sys_key);
remain_encrypt -= 16*1024;
memcpy(data_buf, plain, 16*1024);
}
}
#endif
bFirst = false;
write_count = (src_remain == 0)?dest_remain:dest_step;
bWrited=true;
if(dest_is_parameter) {
printf("write_image: target is parameter!\n");
int i = 0;
for(i = 0; i <= 3; i++) {
printf("write the %d times!\n", i);
lseek(fd_dest, 512*1024*i, SEEK_SET);
if(write(fd_dest, data_buf, dest_is_file?write_count:dest_step) != (dest_is_file?write_count:dest_step) ) {
close(fd_src);
close(fd_dest);
LOGE("Write failed(%s)\n", strerror(errno));
return -1;
}
}
}else {
if(write(fd_dest, data_buf, dest_is_file?write_count:dest_step) != (dest_is_file?write_count:dest_step) ) {
close(fd_src);
close(fd_dest);
LOGE("Write failed(%s)\n", strerror(errno));
return -1;
}
}
dest_remain -= write_count;
// LOGI("src: read=%d remain=%d dest: write=%d remain=%d\n", read_count, src_remain, write_count, dest_remain);
}
close(fd_src);
close(fd_dest);
return 0;
}
int copy_file_from_image(const char* src, const char* dest, int woffset) {
RKIMAGE_HDR *hdr = &g_imagehdr;
bool dest_is_parameter = false;
bool dest_is_file = true;
char destpath[PATH_MAX];
int fd_src, fd_dest;
int src_offset, dest_offset;
char data_buf[16*1024] = {0};
int src_remain, dest_remain;
int src_step, dest_step;
int count = 1;
int src_file_offset = 0;
LOGI("src=%s dest=%s offset=%d\n", src, dest, woffset);
RKIMAGE_ITEM* pItem = (RKIMAGE_ITEM*)FindItem(hdr, src);
if(pItem == NULL)
{
LOGE("Can't find %s \n", src);
return -4;
}
fd_src = open(g_package_target, O_RDONLY);
if (fd_src == 0) {
LOGE("Can't open file: %s\n", g_package_target);
return -5;