-
Notifications
You must be signed in to change notification settings - Fork 3
/
ps3mfw_base.tcl
2905 lines (2515 loc) · 95.7 KB
/
ps3mfw_base.tcl
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
#!/usr/bin/tclsh
#
# ps3mfw -- PS3 MFW creator
#
# Copyright (C) Anonymous Developers (Code Monkeys)
# Copyright (C) RedDot-3ND7355 & ElmerFudd & ThoughtMechanic
#
# This software is distributed under the terms of the GNU General Public
# License ("GPL") version 3, as published by the Free Software Foundation.
#
#
# Keep these allways out!
proc usage {{msg ""}} {
global options
ego
if {$msg != ""} {
puts $msg
}
puts "Usage: ${::argv0} \[options\] <Original> <Modified> \[task\] \[task options\]"
puts "eg. ${::argv0} PS3UPDAT.PUP PS3UPDAT.MFW.PUP"
puts ""
puts "Available options are : "
foreach option [get_sorted_options [file normalize [info script]] [array names options]] {
puts " $option \"$options($option)\"\n [get_option_description [file normalize [info script]] $option]"
}
puts "\nAvailable tasks are : "
foreach task [get_sorted_task_files] {
puts "\nTask:\n--[string map {_ -} [file rootname [file tail $task]]] : [string map {\n \n\t\t\t\t} [get_task_description $task]]"
set taskname [file rootname [file tail $task]]
if { [llength [array names ::${taskname}::options]] } {
puts " Task options:"
foreach option [get_sorted_options $task [array names ::${taskname}::options]] {
puts " $option \"[set ::${taskname}::options($option)]\"\n [get_option_description $task $option]"
}
}
}
puts ""
exit -1
}
proc get_ps3mfw_build {} {
set dldl [open [file join ${::CUSTOM_BUILDVER_TXT}] r]
set versionDL [string trim [read $dldl]]
close $dldl
return $versionDL
}
proc get_ps3mfw_build_alter {} {
set dldl2 [open [file join ${::CUSTOM_BUILDVER_TXT}] r]
set versionDL2 [string trim [read $dldl2]]
close $dldl2
set PS3MFW_BUILD $versionDL2
}
proc debug {msg} {
if {$::options(--debug)} {
log $msg 1
}
}
# End of important processes
# PRINT USAGE
#added array
proc extract_lv0 {path file} {
log "Extracting LV0 and ldr's..."
set fullpath [file join $path $file]
# decrypt LV0 to "LV0.elf", and
# delete the original "lv0"
decrypt_self $fullpath ${fullpath}.elf
file delete ${fullpath}
log "Decrypted lv0!"
# export LV0 contents.....
append fullpath ".elf"
shell ${::LV0TOOL} -option export -filename ${file}.elf -filepath $path
log "Extracted loaders!"
}
# new routine for re-packing LV0 for 3.60+ OFW (using lv0tool.exe)
# default: crypt/decrypt "lv1ldr", unless we are under
# FW version 3.65
proc import_lv0 {path file} {
log "Importing loaders into LV0...."
set ::SELF "lv0"
set fullpath [file join $path $file]
# if firmware is >= 3.65, LV1LDR is crypted, otherwise it's
# not crypted. Also, check if we "override" this setting
# by the flag in the "patch_coreos" task
set FWVer [format "%d%d" $::OFW_MAJOR_VER $::OFW_MINOR_VER]
if { ${FWVer} >= "365"} {
set lv1ldr_crypt "yes"
log "Turned on lv1-crypt"
if {$::FLAG_NO_LV1LDR_CRYPT == "NO" } {
set lv1ldr_crypt "no"
log "Turned off lv1-crypt"
}
}
# execute the "lv0tool" to re-import the loaders
shell ${::LV0TOOL} -option import -lv1crypt $lv1ldr_crypt -cleanup yes -filename ${file}.elf -filepath $path
log "Imported Loaders!"
# resign "lv0.elf" "lv0.self"
sign_elf ${fullpath}.elf ${fullpath}.self
log "Signed lv0!"
file delete lv0.elf
file delete ${fullpath}.elf
file delete appldr.self
file delete isoldr.self
file delete lv1ldr.self
file delete lv2ldr.self
file rename -force ${fullpath}.self $fullpath
}
proc get_log_fd {} {
global log_fd LOG_FILE
if {![info exists log_fd]} {
set log_fd [open $LOG_FILE w]
fconfigure $log_fd -buffering none
}
return $log_fd
}
proc log {msg {force 0}} {
global options
if {!$options(--silent) || $force} {
set fd [get_log_fd]
puts $fd $msg
if {$force} {
puts stderr $msg
} else {
puts $msg
}
}
}
proc debug {msg} {
if {$::options(--debug)} {
log $msg 1
}
}
proc grep {re args} {
set result [list]
set files [eval glob -types f $args]
foreach file $files {
set fp [open $file]
set l 0
while {[gets $fp line] >= 0} {
if [regexp -- $re $line] {
lappend result [list $file $line $l]
}
incr l
}
close $fp
}
set result
}
proc _get_comment_from_file {filename re} {
set results [grep $re $filename]
set comment ""
foreach match $results {
foreach {file match line} $match break
append comment "[string trim [regsub $re $match {}]]\n"
}
string trim $comment
}
proc get_task_description {filename} {
return [_get_comment_from_file $filename {^# Description:}]
}
proc get_option_description {filename option} {
return [_get_comment_from_file $filename "^# Option ${option}:"]
}
proc get_sorted_options {filename options} {
return [lsort -command [list sort_options $filename] $options]
}
proc sort_options {file opt1 opt2 } {
set re1 "^# Option ${opt1}:"
set re2 "^# Option ${opt2}:"
set results1 [grep $re1 $file]
set results2 [grep $re2 $file]
if {$results1 == {} && $results2 == {}} {
return [string compare $opt1 $opt2]
} elseif {$results1 == {}} {
return 1
} elseif {$results2 == {}} {
return -1
} else {
foreach {file match line1} [lindex $results1 0] break
foreach {file match line2} [lindex $results2 0] break
return [expr {$line1 - $line2}]
}
}
proc get_option_type {filename option} {
return [_get_comment_from_file $filename "^# Type ${option}:"]
}
proc task_to_file {task} {
return [file join ${::TASKS_DIR} ${task}.tcl]
}
proc file_to_task {file} {
return [file rootname [file tail $file]]
}
proc compare_tasks {task1 task2} {
return [compare_task_files [task_to_file $task1] [task_to_file $task2]]
}
proc compare_task_files {file1 file2} {
set prio1 [_get_comment_from_file $file1 {^# Priority:}]
set prio2 [_get_comment_from_file $file2 {^# Priority:}]
if {$prio1 == {} && $prio2 == {}} {
return [string compare $file1 $file2]
} elseif {$prio1 == {}} {
return 1
} elseif {$prio2 == {}} {
return -1
} else {
return [expr {$prio1 - $prio2}]
}
}
proc sort_tasks {tasks} {
return [lsort -command compare_tasks $tasks]
}
proc sort_task_files {files} {
return [lsort -command compare_task_files $files]
}
proc get_sorted_tasks { {tasks {}} } {
set files [glob -nocomplain [file join ${::TASKS_DIR} *.tcl]]
set tasks [list]
foreach file $files {
lappend tasks [file_to_task $file]
}
return [sort_tasks $tasks]
}
proc get_sorted_task_files { } {
set files [glob -nocomplain [file join ${::TASKS_DIR} *.tcl]]
return [sort_task_files $files]
}
proc get_selected_tasks { } {
return ${::selected_tasks}
}
# failure function
proc die {message} {
global LOG_FILE
log "FATAL ERROR: $message" 1
puts stderr "See ${LOG_FILE} for more info"
puts stderr "Last lines of log : "
puts stderr "*****************"
catch {puts stderr "[tail $LOG_FILE]"}
puts stderr "*****************"
exit -2
}
proc catch_die {command message} {
set catch {
if {[catch {@command@} res] } {
die "@message@ : $res"
}
return $res
}
debug "Executing command $command"
set catch [string map [list "@command@" "$command" "@message@" "$message"] $catch]
uplevel 1 $catch
}
proc shell {args} {
set fd [get_log_fd]
debug "Executing shell $args"
eval exec $args >&@ $fd
}
proc hexify { str } {
set out ""
for {set i 0} { $i < [string length $str] } { incr i} {
set c [string range $str $i $i]
binary scan $c H* h
append out "\[$h\]"
}
return $out
}
proc tail {filename {n 10}} {
set fd [open $filename r]
set lines [list]
while {![eof $fd]} {
lappend lines [gets $fd]
if {[llength $lines] > $n} {
set lines [lrange $lines end-$n end]
}
}
close $fd
return [join $lines "\n"]
}
proc create_mfw_dir {args} {
catch_die {file mkdir $args} "Could not create dir $args"
}
proc copy_file {args} {
catch_die {file copy {*}$args} "Unable to copy $args"
}
proc copy_dir {src dst } {
debug "Copying source dir:$src to target directory:$dst"
copy_file -force $src $dst
}
proc sha1_check {file} {
shell ${::fciv} -add [file nativename $file] -wp -sha1 -xml db.xml
}
proc sha1_verify {file} {
shell ${::fciv} [file nativename $file] -v -sha1 -xml db.xml
}
proc delete_file {args} {
catch_die {file delete {*}$args} "Unable to delete $args"
}
proc rename_file {src dst} {
catch_die {file rename {*}$src $dst} "Unable to rename and/or move $src $dst"
}
proc delete_promo { } {
delete_file -force ${::CUSTOM_PROMO_FLAGS_TXT}
}
proc copy_spkg { } {
debug "searching for spkg"
set spkg [glob -directory ${::CUSTOM_UPDATE_DIR} *.1]
debug "spkg found in $spkg"
debug "copy new spkg into spkg dir"
copy_file -force $spkg ${::CUSTOM_SPKG_DIR}
if {[file exists [file join $spkg]]} {
debug "removing spkg from working dir"
delete_file -force $spkg
}
}
proc shellex {args} {
set outbuffer ""
debug "Executing shellex $args"
set outbuffer [eval exec $args]
return $outbuffer
}
proc copy_mfw_imgs { } {
create_mfw_dir ${::CUSTOM_MFW_DIR}
copy_file -force ${::CUSTOM_IMG_DIR} ${::CUSTOM_MFW_DIR}
}
# routine to copy standalone '*Install Package Fiels' app into MFW
proc copy_ps3_game {arg} {
variable option
set arg0 $::patch_xmb::options(--add-install-pkg)
set arg1 $::patch_xmb::options(--add-pkg-mgr)
set arg2 $::patch_xmb::options(--add-hb-seg)
set arg3 $::patch_xmb::options(--add-emu-seg)
set arg4 [file exists $::customize_firmware::options(--customize-embedded-app) == 0]
if { $arg0 || $arg1 || $arg2 || $arg3 && !$arg4 } {
rename_file -force $arg ${::CUSTOM_EMBEDDED_APP}
} elseif { $arg0 || $arg1 || $arg2 || $arg3 && $arg4 } {
copy_file -force $arg ${::CUSTOM_MFW_DIR}
} elseif { !$arg0 && !$arg1 && !$arg2 && !$arg3 && !$arg4 } {
create_mfw_dir
rename_file -force $arg ${::CUSTOM_EMBEDDED_APP}
} elseif { !$arg0 && !$arg1 && !$arg2 && !$arg3 && $arg4 } {
create_mfw_dir
copy_file -force $arg ${::CUSTOM_MFW_DIR}
}
unset arg0, arg1, arg2, arg3, arg4
}
proc copy_ps3_game_standart { } {
set ttf "SCE-PS3-RD-R-LATIN.TTF"
debug "using font file .ttf as argument to search for"
debug "cause we need a tar with a bit space in it"
modify_devflash_file [file join dev_flash data font $ttf] callback_ps3_game_standart
}
proc callback_ps3_game_standart { file } {
log "Creating custom directory in dev_flash"
create_mfw_dir ${::CUSTOM_MFW_DIR}
if {${::CFW} == "AC1D"} {
log "Installing standalone 'Custom FirmWare' app"
copy_file -force ${::CUSTOM_PS3_GAME2} ${::CUSTOM_MFW_DIR}
log "Copy custom imgs into dev_flash"
copy_file -force ${::CUSTOM_IMG_DIR} ${::CUSTOM_MFW_DIR}
} else {
log "Installing standalone '*Install Package Files' app"
copy_file -force ${::CUSTOM_PS3_GAME} ${::CUSTOM_MFW_DIR}
}
}
proc pup_extract {pup dest} {
# shell ${::PUP} x $pup $dest
shell ${::PUPUNPACK} [file nativename $pup] [file nativename $dest]
log "Extracted PUP!"
}
proc pup_create {dir pup build} {
# shell ${::PUP} c $dir $pup $build
shell ${::PUPPACK} $pup [file nativename $dir] [file nativename $build]
log "Created PUP!"
}
proc pup_get_build {pup} {
set fd [open $pup r]
fconfigure $fd -translation binary
seek $fd 16
set build [read $fd 8]
close $fd
if {[binary scan $build W build_ver] != 1} {
error "Cannot read 64 bit big endian from [hexify $build]"
}
return $build_ver
}
proc extract_tar {tar dest} {
file mkdir $dest
debug "Extracting tar file [file tail $tar] into [file tail $dest]"
catch_die {::tar::untar $tar -dir $dest} "Could not untar file $tar"
}
proc create_tar {tar directory files} {
set debug [file tail $tar]
if {$debug == "content" } {
set debug [file tail [file dirname $tar]]
}
debug "Creating tar file $debug"
set pwd [pwd]
cd $directory
catch_die {::tar::create $tar $files} "Could not create tar file $tar"
cd $pwd
}
proc find_devflash_archive {dir find} {
foreach file [glob -nocomplain [file join $dir * content]] {
if {[catch {::tar::stat $file $find}] == 0} {
return $file
}
}
return ""
}
proc spkg {pkg} {
shell ${::SPKG} [file nativename $pkg]
}
proc new_pkg {pkg dest} {
log "Making spkg retail"
shell ${::NEWPKG} retail [file nativename $pkg] [file nativename $dest]
log "Builded"
}
proc unpkg {pkg dest} {
shell ${::UNPKG} [file nativename $pkg] [file nativename $dest]
}
proc pkg_spkg_archive {dir pkg} {
debug "pkg-ing / spkg-ing file [file tail $pkg]"
catch_die {pkg_spkg $dir $pkg} "Could not pkg / spkg file [file tail $pkg]"
}
proc pkg {pkg dest} {
shell ${::PKG} retail [file nativename $pkg] [file nativename $dest]
}
proc unpkg_archive {pkg dest} {
debug "unpkg-ing file [file tail $pkg]"
catch_die {unpkg $pkg $dest} "Could not unpkg file [file tail $pkg]"
log "Extracted"
}
proc pkg_archive {dir pkg} {
debug "pkg-ing file [file tail $pkg]"
catch_die {pkg $dir $pkg} "Could not pkg file [file tail $pkg]"
log "Imported"
}
proc new_pkg_archive {dir pkg} {
debug "pkg-ing / spkg-ing file [file tail $pkg]"
catch_die {new_pkg $dir $pkg} "Could not pkg / spkg file [file tail $pkg]"
log "Created"
}
proc spkg_archive {pkg} {
debug "spkg-ing file [file tail $pkg]"
catch_die {spkg $dir $pkg} "Could not spkg file [file tail $pkg]"
log "Imported SPKG"
}
proc unpkg_devflash_all {dir} {
file mkdir $dir
foreach file [lsort [glob -nocomplain [file join ${::CUSTOM_UPDATE_DIR} dev_flash_*]]] {
unpkg_archive $file [file join $dir [file tail $file]]
}
log "Unpacked Devflash!"
}
proc cosunpkg { pkg dest } {
shell ${::COSUNPKG} [file nativename $pkg] [file nativename $dest]
}
proc cospkg { dir pkg } {
shell ${::COSPKG} [file nativename $pkg] [file nativename $dir]
}
proc cosunpkg_package { pkg dest } {
debug "cosunpkg-ing file [file tail $pkg]"
catch_die { cosunpkg $pkg $dest } "Could not cosunpkg file [file tail $pkg]"
log "Unpacked"
}
proc cospkg_package { dir pkg } {
debug "cospkg-ing file [file tail $dir]"
catch_die { cospkg $dir $pkg } "Could not cospkg file [file tail $pkg]"
log "Packed"
}
proc modify_coreos_file { file callback args } {
log "Modifying CORE_OS file [file tail $file]"
set pkg [file join ${::CUSTOM_UPDATE_DIR} CORE_OS_PACKAGE.pkg]
set unpkgdir [file join ${::CUSTOM_UPDATE_DIR} CORE_OS_PACKAGE.unpkg]
set cosunpkgdir [file join ${::CUSTOM_UPDATE_DIR} CORE_OS_PACKAGE]
::unpkg_archive $pkg $unpkgdir
::cosunpkg_package [file join $unpkgdir content] $cosunpkgdir
if {[file writable [file join $cosunpkgdir $file]]} {
set ::SELF $file
eval $callback [file join $cosunpkgdir $file] $args
} elseif { ![file exists [file join $cosunpkgdir $file]] } {
die "Could not find $file in CORE_OS_PACKAGE"
} else {
die "File $file is not writable in CORE_OS_PACKAGE"
}
::cospkg_package $cosunpkgdir [file join $unpkgdir content]
if {[::get_pup_version] >= ${::NEWCFW}} {
::new_pkg_archive $unpkgdir $pkg
::copy_spkg
} else {
::pkg_archive $unpkgdir $pkg
}
}
proc modify_coreos_files { files callback args } {
log "Modifying CORE_OS files [file tail $files]"
set pkg [file join ${::CUSTOM_UPDATE_DIR} CORE_OS_PACKAGE.pkg]
set unpkgdir [file join ${::CUSTOM_UPDATE_DIR} CORE_OS_PACKAGE.unpkg]
set cosunpkgdir [file join ${::CUSTOM_UPDATE_DIR} CORE_OS_PACKAGE]
::unpkg_archive $pkg $unpkgdir
::cosunpkg_package [file join $unpkgdir content] $cosunpkgdir
foreach file $files {
if {[file writable [file join $cosunpkgdir $file]]} {
log "Using file $file now"
set ::SELF $file
eval $callback [file join $cosunpkgdir $file] $args
} elseif { ![file exists [file join $cosunpkgdir $file]] } {
die "Could not find $file in CORE_OS_PACKAGE"
} else {
die "File $file is not writable in CORE_OS_PACKAGE"
}
}
::cospkg_package $cosunpkgdir [file join $unpkgdir content]
if {[::get_pup_version] >= ${::NEWCFW}} {
::new_pkg_archive $unpkgdir $pkg
::copy_spkg
} else {
::pkg_archive $unpkgdir $pkg
}
}
# proc for "unpackaging" the "CORE_OS" files
proc unpack_coreos_files { args } {
#::CUSTOM_PKG_DIR == $pkg
#::CUSTOM_UNPKG_DIR == $unpkg
#::CUSTOM_COSUNPKG_DIR == $cosunpkg
log "Unpacking CORE_OS files..."
::unpkg_archive $::CUSTOM_PKG_DIR $::CUSTOM_UNPKG_DIR
::cosunpkg_package [file join $::CUSTOM_UNPKG_DIR content] $::CUSTOM_COSUNPKG_DIR
# set the global flag that "CORE_OS" is unpacked
set ::FLAG_COREOS_UNPACKED 1
log "CORE_OS Unpacked!"
}
# proc for "packaging" up the "CORE_OS" files
proc repack_coreos_files { args } {
#::CUSTOM_PKG_DIR == $pkg
#::CUSTOM_UNPKG_DIR == $unpkg
#::CUSTOM_COSUNPKG_DIR == $cosunpkg
log "Repacking CORE_OS files..."
# re-package up the files
::cospkg_package $::CUSTOM_COSUNPKG_DIR [file join $::CUSTOM_UNPKG_DIR content]
set pkg $::CUSTOM_PKG_DIR
set unpkgdir $::CUSTOM_UNPKG_DIR
if {[::get_pup_version] >= ${::NEWCFW}} {
::new_pkg_archive $unpkgdir $pkg
::copy_spkg
} else {
::pkg_archive $unpkgdir $pkg
}
# set the global flag that "CORE_OS" is packed
set ::FLAG_COREOS_UNPACKED 0
log "CORE_OS Repacked!"
}
proc get_pup_build {} {
debug "Getting PUP build from [file tail ${::IN_FILE}]"
catch_die {pup_get_build ${::IN_FILE}} "Could not get the PUP build information"
return [pup_get_build ${::IN_FILE}]
log "PUP Build grabbed!"
}
proc set_pup_build {build} {
debug "PUP build: $build"
set ::PUP_BUILD $build
}
proc get_pup_version {} {
debug "Getting PUP version from [file tail ${::CUSTOM_VERSION_TXT}]"
set fd [open [file join ${::CUSTOM_VERSION_TXT}] r]
set version [string trim [read $fd]]
close $fd
return $version
log "PUP Version Grabbed!"
}
proc set_pup_version {version} {
debug "Setting PUP version in [file tail ${::CUSTOM_VERSION_TXT}]"
set fd [open [file join ${::CUSTOM_VERSION_TXT}] w]
puts $fd "${version}"
close $fd
}
proc modify_pup_version_file {prefix suffix {clear 0}} {
if {$clear} {
set version ""
} else {
set version [::get_pup_version]
}
debug "PUP version: ${prefix}${version}${suffix}"
set_pup_version "${prefix}${version}${suffix}"
}
proc sed_in_place {file search replace} {
set fd [open $file r]
set data [read $fd]
close $fd
set data [string map [list $search $replace] $data]
set fd [open $file w]
puts -nonewline $fd $data
close $fd
}
proc unself {in out} {
set FIN [file nativename $in]
set FOUT [file nativename $out]
shell ${::SCETOOL} -d $FIN $FOUT
log "Decrypted!"
}
proc decrypt_self {in out} {
debug "Decrypting self file [file tail $in]"
catch_die {unself $in $out} "Could not decrypt file [file tail $in]"
log "Self Decrypted!"
}
proc import_self_info {in array} {
log "Importing self-hdr info from file: [file tail $in]"
upvar $array MySelfHdrs
set MyArraySize 0
# clear the incoming array
foreach key [array names MySelfHdrs] {
set MySelfHdrs($key) ""
}
# execute the "SCETOOL -w" cmd to dump the needed SCE-HDR info
catch_die {set buffer [shellex ${::SCETOOL2} -w $in]} "failed to dump SCE header for file: [file tail $in]"
# parse out the return buffer, and
# save off the fields into the global array
set data [split $buffer "\n"]
foreach line $data {
if [regexp -- {(^Key-Revision:)(.*)} $line match] {
set MySelfHdrs(--KEYREV) [lindex [split $match ":"] 1]
incr MyArraySize 1
} elseif { [regexp -- {(^Auth-ID:)(.*)} $line match] } {
set MySelfHdrs(--AUTHID) [lindex [split $match ":"] 1]
incr MyArraySize 1
} elseif { [regexp -- {(^Vendor-ID:)(.*)} $line match] } {
set MySelfHdrs(--VENDORID) [lindex [split $match ":"] 1]
incr MyArraySize 1
} elseif { [regexp -- {(^SELF-Type:)(.*)} $line match] } {
set MySelfHdrs(--SELFTYPE) [lindex [split $match ":"] 1]
incr MyArraySize 1
} elseif { [regexp -- {(^AppVersion:)(.*)} $line match] } {
set MySelfHdrs(--APPVERSION) [lindex [split $match ":"] 1]
incr MyArraySize 1
} elseif { [regexp -- {(^FWVersion:)(.*)} $line match] } {
set MySelfHdrs(--FWVERSION) [lindex [split $match ":"] 1]
incr MyArraySize 1
} elseif { [regexp -- {(^CtrlFlags:)(.*)} $line match] } {
set MySelfHdrs(--CTRLFLAGS) [lindex [split $match ":"] 1]
incr MyArraySize 1
} elseif { [regexp -- {(^CapabFlags:)(.*)} $line match] } {
set MySelfHdrs(--CAPABFLAGS) [lindex [split $match ":"] 1]
incr MyArraySize 1
} elseif { [regexp -- {(^Compressed:)(.*)} $line match] } {
set MySelfHdrs(--COMPRESS) [lindex [split $match ":"] 1]
incr MyArraySize 1
}
}
# if we successfully captured all vars,
# and it matches our array size, success
if { $MyArraySize == [array size MySelfHdrs] } {
log "Self-sce hdr imported successfully!"
} else {
log "!!ERROR!!: FAILED TO IMPORT SELF-SCE HEADERS FROM FILE: [file tail $in]"
die "!!ERROR!!: FAILED TO IMPORT SELF-SCE HEADERS FROM FILE: [file tail $in]"
}
# display the imported headers if VERBOSE enabled
if { $::options(--task-verbose) } {
foreach key [lsort [array names MySelfHdrs]] {
log "-->$key:$MySelfHdrs($key)"
}
}
}
proc sign_elf {in out} {
debug "Rebuilding self file [file tail $out]"
catch_die {makeself $in $out} "Could not rebuild file [file tail $out]"
log "Self Signed"
}
proc modify_self_file {file callback args} {
log "Modifying self/sprx file [file tail $file]"
decrypt_self $file ${file}.elf
eval $callback ${file}.elf $args
sign_elf ${file}.elf ${file}.self
#file copy -force ${file}.self ${::BUILD_DIR} # used for debugging to copy the patched elf and new re-signed self to MFW build dir without the need to unpup the whole fw or even a single file
file rename -force ${file}.self $file
#file copy -force ${file}.elf ${::BUILD_DIR} # same as above
file delete ${file}.elf
log "Self successful rebuilded"
}
proc patch_self {file search replace_offset replace {ignore_bytes {}}} {
modify_self_file $file patch_elf $search $replace_offset $replace $ignore_bytes
}
proc patch_elf {file search replace_offset replace {ignore_bytes {}}} {
patch_file $file $search $replace_offset $replace $ignore_bytes
}
proc patch_file {file search replace_offset replace {ignore_bytes {}}} {
foreach bytes $ignore_bytes {
if {[llength $bytes] == 1} {
set search [string replace $search $bytes $bytes "?"]
} elseif {[llength $bytes] == 2} {
set idx1 [lindex $bytes 0]
set idx2 [lindex $bytes 1]
set len [expr {$idx2 - $idx1 + 1}]
if {$len < 0} {
set len 0
}
set search [string replace $search $idx1 $idx2 [string repeat "?" $len]]
}
}
set fd [open $file r+]
fconfigure $fd -translation binary
set offset -1
set buffer ""
while {![eof $fd]} {
append buffer [read $fd 1]
if {[string length $buffer] > [string length $search]} {
set buffer [string range $buffer 1 end]
}
set tmp $buffer
foreach bytes $ignore_bytes {
if {[llength $bytes] == 1} {
set tmp [string replace $tmp $bytes $bytes "?"]
} elseif {[llength $bytes] == 2} {
set idx1 [lindex $bytes 0]
set idx2 [lindex $bytes 1]
set len [expr {$idx2 - $idx1 + 1}]
if {$len < 0} {
set len 0
}
set tmp [string replace $tmp $idx1 $idx2 [string repeat "?" $len]]
}
}
if {$tmp == $search} {
if {$offset != -1} {
error "Pattern found multiple times"
}
set offset [tell $fd]
incr offset -[string length $search]
incr offset $replace_offset
}
}
if {$offset == -1} {
error "Could not find pattern to patch"
}
debug "offset: $offset"
seek $fd $offset
puts -nonewline $fd $replace
close $fd
}
proc patch_file_multi {file search replace_offset replace {ignore_bytes {}}} {
foreach bytes $ignore_bytes {
if {[llength $bytes] == 0} {
set search [string replace $search $bytes $bytes "?"]
} else {
set search [string replace $search [lindex $bytes 0] [lindex $bytes 1] "?"]
}
}
set fd [open $file r+]
fconfigure $fd -translation binary
set offset -1
set counter 0
set buffer ""
while {![eof $fd]} {
append buffer [read $fd 1]
if {[string length $buffer] > [string length $search]} {
set buffer [string range $buffer 1 end]
}
set tmp $buffer
foreach bytes $ignore_bytes {
if {[llength $bytes] == 0} {
set tmp [string replace $tmp $bytes $bytes "?"]
} else {
set tmp [string replace $tmp [lindex $bytes 0] [lindex $bytes 1] "?"]
}
}
if {$tmp == $search} {
incr counter 1
set offset [tell $fd]
incr offset -[string length $search]
incr offset $replace_offset
debug "offset: $offset"
seek $fd $offset
puts -nonewline $fd $replace
seek $fd $offset
set offset -1
}
}
if {$counter == 0} {
debug "Could not find pattern to patch"
} else {
debug "Replaced $counter occurences of search pattern"
}
close $fd
}
proc modify_devflash_file {file callback args} {
log "Modifying dev_flash file [file tail $file]"
set tar_file [find_devflash_archive ${::CUSTOM_DEVFLASH_DIR} $file]
if {$tar_file == ""} {
die "Could not find [file tail $file]"
}
set pkg_file [file tail [file dirname $tar_file]]
debug "Found [file tail $file] in $pkg_file"
file delete -force [file join ${::CUSTOM_DEVFLASH_DIR} dev_flash]
extract_tar $tar_file ${::CUSTOM_DEVFLASH_DIR}
if {[file writable [file join ${::CUSTOM_DEVFLASH_DIR} $file]] } {
eval $callback [file join ${::CUSTOM_DEVFLASH_DIR} $file] $args
} elseif { ![file exists [file join ${::CUSTOM_DEVFLASH_DIR} $file]] } {
die "Could not find $file in ${::CUSTOM_DEVFLASH_DIR}"
} else {
die "File $file is not writable in ${::CUSTOM_DEVFLASH_DIR}"
}
file delete -force $tar_file
create_tar $tar_file ${::CUSTOM_DEVFLASH_DIR} dev_flash
set pkg [file join ${::CUSTOM_UPDATE_DIR} $pkg_file]
set unpkgdir [file join ${::CUSTOM_DEVFLASH_DIR} $pkg_file]
if {[::get_pup_version] >= ${::NEWCFW}} {
::new_pkg_archive $unpkgdir $pkg
::copy_spkg
} else {
::pkg_archive $unpkgdir $pkg
}
}
proc modify_devflash_files {path files callback args} {
foreach file $files {
set file [file join $path $file]
log "Modifying dev_flash file [file tail $file]"
set tar_file [find_devflash_archive ${::CUSTOM_DEVFLASH_DIR} $file]
if {$tar_file == ""} {
debug "Skipping [file tail $file] not found"
continue
}
set pkg_file [file tail [file dirname $tar_file]]
debug "Found [file tail $file] in $pkg_file"
file delete -force [file join ${::CUSTOM_DEVFLASH_DIR} dev_flash]
extract_tar $tar_file ${::CUSTOM_DEVFLASH_DIR}
if {[file writable [file join ${::CUSTOM_DEVFLASH_DIR} $file]] } {
set ::SELF $file
log "Using file $file now"
eval $callback [file join ${::CUSTOM_DEVFLASH_DIR} $file] $args
} elseif { ![file exists [file join ${::CUSTOM_DEVFLASH_DIR} $file]] } {
debug "Could not find $file in ${::CUSTOM_DEVFLASH_DIR}"
} else {
die "File $file is not writable in ${::CUSTOM_DEVFLASH_DIR}"
}
file delete -force $tar_file
create_tar $tar_file ${::CUSTOM_DEVFLASH_DIR} dev_flash
set pkg [file join ${::CUSTOM_UPDATE_DIR} $pkg_file]
set unpkgdir [file join ${::CUSTOM_DEVFLASH_DIR} $pkg_file]
if {[::get_pup_version] >= ${::NEWCFW}} {
::new_pkg_archive $unpkgdir $pkg
::copy_spkg
} else {
::pkg_archive $unpkgdir $pkg
}
}
}