aboutsummaryrefslogtreecommitdiffstats
path: root/recipes-bsp/zaurus-utils/zaurus-updater/updater.sh
blob: c20c5db418cbdc031c01eff5beaa56ae92d4f913 (plain)
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
#!/bin/sh
#
# One updater.sh to rule them all
#
# 2006.10.24 Marcin 'Hrw' Juszkiewicz
# - started work on common updater.sh
# - works on poodle, c760, spitz
# - breaks on tosa
#
# 2007.10.08 Marcin 'Hrw' Juszkiewicz
# - do not allow to flash files bigger then partition size
# - created functions for common stuff
#
# 2007.11.18 Dmitry 'Lumag' Baryshkov
# - fixes
# - tosa unbreak
#
# 2007.11.19 Marcin 'Hrw' Juszkiewicz
# - size check unbreak
# - c760/c860 has bigger rootfs - use it
#
# 2007.11.23 Koen Kooi
# - consistent error messages
# - fix flashing from case sensitive filesystem (e.g. ext2)
#
# 2007.11.23 Matthias 'CoreDump' Hentges
# - Always treat MTD_PART_SIZE as HEX when comparing sizes
# - Thanks to ZeroChaos for debugging
#
# 2007.12.04 Matthias 'CoreDump' Hentges
# - Unb0rk flashing of Akita kernels
#
# 2007.12.10 Marcin 'Hrw' Juszkiewicz
# - Reformatted file - please use spaces not tabs
# - "version check" is only on Tosa and Poodle - breaks other machines
#
# 2007.12.23 Matthias 'CoreDump' Hentges
# - Fix kernel install on spitz machines
# - Unify format of do_flashing()...
# - Display ${PR} of zaurus-updater.bb to the user
# - Polish HDD installer messages
#
# 2007.12.25 Matthias 'CoreDump' Hentges
# - Add support for installing / updating u-boot
#
# 2008.11.23 Dmitry 'lumag' Baryshkov
# - Add support for reflashing home partitions
#
# 2010.02.02 Andrea 'ant' Adami
# - Fix nandlogical writing of kernel
#   Bug in Sharp original line...
#   /sbin/nandlogical $LOGOCAL_MTD WRITE $ADDR $DATASIZE $TMPDATA > /dev/null 2>&1
#   didn't use correctly determined block size
#
# 2010.04.18 Andrea 'ant' Adami
# - Add support for flashing pure jffs2 rootfs, without Sharp headers
#
# 2017.05.05 Andrea 'ant' Adami
# - Fix RW_MTD_SIZE
# - Fix RO_MTD_LINE in case of hardcoded partitioning
# - Define SMF_MTD_* instead of hardcoding, replace LOGOCAL_MTD
# - Add support for repartitioning
# - Limit screen output to 30 chars
#
# 2017.05.17 Andrea 'ant' Adami
# - Major Version bump: 2017.05
# - do not hardcode smf/logocal = mtd1
# - check total flash size
# - add Partition Manager: view, resize, erase partitions

DATAPATH=$1
TMPPATH=/tmp/update
TMPDATA=$TMPPATH/tmpdata.bin
TMPHEAD=$TMPPATH/tmphead.bin

FLASHED_MAINTE=0
FLASHED_KERNEL=0
FLASHED_ROOTFS=0
FLASHED_HOMEFS=0
UNPACKED_ROOTFS=0   # spitz only

SMF_MTD_LINE=`cat /proc/mtd | grep "smf" | tail -n 1`
if [ "$SMF_MTD_LINE" = "" ]; then
    SMF_MTD_LINE=`cat /proc/mtd | grep "\<NAND\>.*\<0\>" | tail -n 1`
fi
SMF_MTD_NO=`echo $SMF_MTD_LINE | cut -d: -f1 | cut -dd -f2`
SMF_MTD=/dev/mtd$SMF_MTD_NO
SMF_SIZE=`echo $SMF_MTD_LINE | cut -d" " -f2`
SMF_SIZE_MB=`dc 0x$SMF_SIZE 1024 / 1024 /`

RO_MTD_LINE=`cat /proc/mtd | grep "root" | tail -n 1`
if [ "$RO_MTD_LINE" = "" ]; then
    RO_MTD_LINE=`cat /proc/mtd | grep "\<NAND\>.*\<1\>" | tail -n 1`
fi
RO_MTD_NO=`echo $RO_MTD_LINE | cut -d: -f1 | cut -dd -f2`
RO_MTD=/dev/mtd$RO_MTD_NO
ROOTFS_SIZE=`echo $RO_MTD_LINE | cut -d" " -f2`
ROOTFS_SIZE_MB=`dc 0x$ROOTFS_SIZE 1024 / 1024 /`

RW_MTD_LINE=`cat /proc/mtd | grep "home" | tail -n 1`
if [ "$RW_MTD_LINE" = "" ]; then
    RW_MTD_LINE=`cat /proc/mtd | grep "\<NAND\>.*\<2\>" | tail -n 1`
fi
RW_MTD_NO=`echo $RW_MTD_LINE | cut -d: -f1 | cut -dd -f2`
RW_MTD=/dev/mtd$RW_MTD_NO
HOMEFS_SIZE=`echo $RW_MTD_LINE | cut -d" " -f2`
HOMEFS_SIZE_MB=`dc 0x$HOMEFS_SIZE 1024 / 1024 /`

MAX_FSRO_SIZE_MB=`expr $ROOTFS_SIZE_MB + $HOMEFS_SIZE_MB - 1`

VERBLOCK=0x48000
MVRBLOCK=0x70000

RESULT=0
SEP="------------------------------"

Cleanup()
{
    rm -f $VTMPNAME > /dev/null 2>&1
    rm -f $MTMPNAME > /dev/null 2>&1
    exit $1
}

trap 'Cleanup 1' 1 15
trap '' 2 3

get_dev_pcmcia()
{
    while read SOCKET CLASS DRIVER INSTANCE DEVS MAJOR MINOR;
    do
        echo $DEVS
    done
}

get_dev_pcmcia_slot()
{
    grep "^$1" /var/lib/pcmcia/stab | get_dev_pcmcia
}

check_for_hdd()
{
    IDE1=`get_dev_pcmcia_slot 1`
    if [ "$IDE1" = "" ]; then
        echo 'Error: There is no microdrive. Retrying...'
        while [ "$IDE1" = "" ]; do
            IDE1=`get_dev_pcmcia_slot 1`
        done
        echo 'Microdrive found.'
    fi

    LINUXFMT=ext3
    MKE2FSOPT=-j
}

check_for_tar()
{
    ### Check that we have a valid tar
    for TARNAME in gnu-tar GNU-TAR
    do
        if [ -e $DATAPATH/$TARNAME ]
        then
            TARBIN=$DATAPATH/$TARNAME
        fi
    done

    if [ ! -e $TARBIN ]; then
        echo 'Error: Please place a valid copy of tar as "gnu-tar" on your card.'
        echo 'Please reset'
        while true
        do
        done
    fi
}

do_rootfs_extraction()
{
    UNPACKED_ROOTFS=1
    echo 'Installing HDD root file system'
    if [ ! -f /hdd1/NotAvailable ]; then
        umount /hdd1
    fi
    echo -n '* Now formatting...'
    mke2fs $MKE2FSOPT /dev/${IDE1}1 > /dev/null 2>&1
    e2fsck -p /dev/${IDE1}1 > /dev/null
    if [ "$?" != "0" ]; then
        echo 'FAILED'
        echo 'Error: Unable to create filesystem on microdrive!'
        exit "$?"
    else 
        echo 'done'
    fi

    mount -t $LINUXFMT -o noatime /dev/${IDE1}1 /hdd1
    if [ "$?" != "0" ]; then
        echo 'Error: Unable to mount microdrive!'
        exit "$?"
    fi

    cd /hdd1
    echo -n '* Now extracting (this can take over 5m)...'
    gzip -dc $DATAPATH/$TARGETFILE | $TARBIN xf -
    if [ "$?" != "0" ]; then
        echo 'FAILED'
        echo 'Error: Unable to extract root filesystem archive!'
        exit "$?"
    else
        echo 'done'
    fi

    echo 'HDD Installation Finished.'

    # remount as RO
    cd /
    umount /hdd1
    mount -t $LINUXFMT -o ro,noatime /dev/${IDE1}1 /hdd1
}

do_flashing()
{
        if [ $DATASIZE -gt `printf "%d" $MTD_PART_SIZE` ]
        then
                echo 'Error: File is too big to flash!'
                echo "$FLASH_TYPE: [$DATASIZE] > [`printf "%d" ${MTD_PART_SIZE}`]"
                return
        fi

        #check version (common to all models)
        /sbin/bcut -s 6 -o $TMPDATA $TMPHEAD
        if [ `cat $TMPDATA` != "SHARP!" ] > /dev/null 2>&1
        then
            #check for known fake headers
            if [ `cat $TMPDATA` != "OZ!3.1" ] > /dev/null 2>&1
            then
                #no version info...
                rm -f $TMPHEAD > /dev/null 2>&1
                DATAPOS=0
            fi
        fi

        if [ $ISFORMATTED = 0 ]
        then
                echo -n 'Flash erasing...'
                /sbin/eraseall $TARGET_MTD > /dev/null 2>&1
                echo 'done'
                ISFORMATTED=1
        fi

        if [ -e $TMPHEAD ]
        then
                VTMPNAME=$TMPPATH'/vtmp'`date '+%s'`'.tmp'
                MTMPNAME=$TMPPATH'/mtmp'`date '+%s'`'.tmp'
                /sbin/nandlogical $SMF_MTD READ $VERBLOCK 0x4000 $VTMPNAME > /dev/null 2>&1
                /sbin/nandlogical $SMF_MTD READ $MVRBLOCK 0x4000 $MTMPNAME > /dev/null 2>&1

                /sbin/verchg -v $VTMPNAME $TMPHEAD $MODULEID $MTD_PART_SIZE > /dev/null 2>&1
                /sbin/verchg -m $MTMPNAME $TMPHEAD $MODULEID $MTD_PART_SIZE > /dev/null 2>&1
        fi

        # Looks like Akita and Spitz are unique when it comes to kernel flashing
        if [ "$ZAURUS" = "akita" -o "$ZAURUS" = "c3x00" ] && [ "$FLASH_TYPE" = "kernel" ]
        then
                echo ''
                echo -n 'Installing SL-Cxx00 kernel...'
                echo '                ' > /tmp/data
                test "$ZAURUS" = "akita" && /sbin/nandlogical $SMF_MTD WRITE 0x60100 16 /tmp/data > /dev/null 2>&1
                /sbin/nandlogical $SMF_MTD WRITE 0xe0000 $DATASIZE $TARGETFILE > /dev/null 2>&1
                test "$ZAURUS" = "akita" && /sbin/nandlogical $SMF_MTD WRITE 0x21bff0 16 /tmp/data > /dev/null 2>&1
                echo 'done'
        else
                echo ''
                echo '0%                   100%'
                PROGSTEP=`expr $DATASIZE / $ONESIZE + 1`
                PROGSTEP=`expr 25 / $PROGSTEP`

                if [ $PROGSTEP = 0 ]
                then
                    PROGSTEP=1
                fi

                #loop
                while [ $DATAPOS -lt $DATASIZE ]
                do
                        #data create
                        bcut -a $DATAPOS -s $ONESIZE -o $TMPDATA $TARGETFILE
                        TMPSIZE=`wc -c $TMPDATA`
                        TMPSIZE=`echo $TMPSIZE | cut -d' ' -f1`
                        DATAPOS=`expr $DATAPOS + $TMPSIZE`

                        #handle data file
                        if [ $ISLOGICAL = 0 ]
                        then
                                next_addr=`/sbin/nandcp -a $ADDR $TMPDATA $TARGET_MTD  2>/dev/null | fgrep "mtd address" | cut -d- -f2 | cut -d\( -f1`
                                if [ "$next_addr" = "" ]; then
                                        echo 'Error: flash write'
                                        rm $TMPDATA > /dev/null 2>&1
                                        RESULT=3
                                        break;
                                fi
                                ADDR=$next_addr
                        else
                                /sbin/nandlogical $SMF_MTD WRITE $ADDR $TMPSIZE $TMPDATA > /dev/null 2>&1
                                ADDR=`expr $ADDR + $TMPSIZE`
                        fi

                        rm $TMPDATA > /dev/null 2>&1

                        #progress
                        SPNUM=0
                        while [ $SPNUM -lt $PROGSTEP ]
                        do
                                echo -n '.'
                                SPNUM=`expr $SPNUM + 1`
                        done
                done
        fi
        echo ''

        #finish
        rm -f $TMPPATH/*.bin > /dev/null 2>&1

        if [ $RESULT = 0 ]
        then
                if [ -e $VTMPNAME ]
                then
                    /sbin/nandlogical $SMF_MTD WRITE $VERBLOCK 0x4000 $VTMPNAME > /dev/null 2>&1
                    rm -f $VTMPNAME > /dev/null 2>&1
                fi

                if [ -e $MTMPNAME ]
                then
                    /sbin/nandlogical $SMF_MTD WRITE $MVRBLOCK 0x4000 $MTMPNAME > /dev/null 2>&1
                    rm -f $MTMPNAME > /dev/null 2>&1
                fi

                [ "$FLASH_TYPE" != "kernel" ] && echo 'done.'
        else
                echo 'Error!'
        fi
}

mainte_fix()
{
    # binaries taken from Cacko 1.23 installer
    case "$MODEL" in
        SL-C760|SL-C860|SL-C1000|SL-C3100|SL-C3200)
            echo -n 'Flashing Mainte fix for 128M flash...'
            if ( /sbin/nandlogical $SMF_MTD WRITE 0x00, 327680, $TARGETFILE ) > /dev/null 2>&1
            then
                echo 'done'
                # unnecessary fix for FSRW end addr / size ? (kernel parser sharpslpart ignores it)
                # OUTFILE=$TMPPATH/128M_fix.bin
                # printf '\x08' > $OUTFILE
                # /sbin/nandlogical $LOGOCAL_MTD WRITE 0x60027, 1, $OUTFILE > /dev/null 2>&1
                # /sbin/nandlogical $LOGOCAL_MTD WRITE 0x64027, 1, $OUTFILE > /dev/null 2>&1
                # rm -f $OUTFILE > /dev/null 2>&1
                exit 0
            fi
            ;;
        *)
            ;;
    esac
}

check_partitions()
{
    if [ $FLASH_SIZE_MB -gt `expr $SMF_SIZE_MB + $ROOTFS_SIZE_MB + $HOMEFS_SIZE_MB` ]
    then
        printf "%b\n" "$SEP\n\033[1;31mFlash size mismatch!\033[0m\nThis unit is repartitioned and\nyou \033[1;31mmust\033[0m flash  \033[1;33mmainte_fix.bin\033[0m"
        printf "%b\n" "to keep this partition layout,\notherwise resize root to \033[1;32m$DEF_ROOTFS_SIZE_MB\033[0m MB"
        printf "%b\n" "\nReset to factory settings  can\nbe done entering  \033[1;33mService Menu\033[0m\nand running \033[1;32mNAND Flash Restore\033[0m\n$SEP\n"
    fi
}

partition_manager()
{
    while true; do
        ANSW=""
        read -p "Partition Manager? (y/n): " ANSW
        if expr "$ANSW" : '[Yy]\+$' > /dev/null 2>&1
        then
            break
        elif expr "$ANSW" : '[Nn]\+$' > /dev/null 2>&1
        then
            return
        fi
    done

    clear
    printf "%b\n" "\n\033[1;31mZAURUS\033[1;32m PARTITION MANAGER\033[0m\n$SEP\n1 View\n2 Resize\n3 Erase\n\n0 Exit\n$SEP"

    while true; do
        ANSW=""
        read ANSW

        case "$ANSW" in
            1)  view_partitions
                ;;
            2)  change_partitions
                ;;
            3)  erase_partitions
                ;;
            0)  exit 0
                ;;
            *)
                ;;
        esac
    done
}

view_partitions()
{
    printf "%b\n" "$SEP\nPartition  \033[1msmf    root    home\033[0m"
    printf "%b\n" "Size (MB)   \033[1;32m$SMF_SIZE_MB      $ROOTFS_SIZE_MB      $HOMEFS_SIZE_MB\033[0m\n$SEP"
}


erase_partitions()
{
    while true; do
        ANSW=""
        read -p "Erase partitions? (y/n): " ANSW
        if expr "$ANSW" : '[Yy]\+$' > /dev/null 2>&1
        then
            break
        elif expr "$ANSW" : '[Nn]\+$' > /dev/null 2>&1
        then
            return
        fi
    done

    echo -n 'Erasing root partition... '
    /sbin/eraseall $RO_MTD > /dev/null 2>&1
    echo 'done'
    echo -n 'Erasing home partition... '
    /sbin/eraseall $RW_MTD > /dev/null 2>&1
    echo 'done'
}

change_partitions()
{
    while true; do
        ANSW=""
        read -p "Resize partitions? (y/n): " ANSW
        if expr "$ANSW" : '[Yy]\+$' > /dev/null 2>&1
        then
            break
        elif expr "$ANSW" : '[Nn]\+$' > /dev/null 2>&1
        then
            return
        fi
    done

    while true; do
        FSRO_SIZE_MB=""
        read -p "New root size (1-$MAX_FSRO_SIZE_MB): " FSRO_SIZE_MB
        if expr "$FSRO_SIZE_MB" : '[0-9]\+$' > /dev/null 2>&1 &&
           [ $FSRO_SIZE_MB -gt 0 ] > /dev/null 2>&1 &&
           [ $FSRO_SIZE_MB -le $MAX_FSRO_SIZE_MB ] > /dev/null 2>&1
        then
            break
        fi
    done

    FSRW_SIZE_MB=`expr $ROOTFS_SIZE_MB + $HOMEFS_SIZE_MB - $FSRO_SIZE_MB`

    FSRO_END_B=`expr $(($(($SMF_SIZE_MB + $FSRO_SIZE_MB)) << 20))`

    B1=`printf '%08x' $FSRO_END_B | cut -c1-2`
    B2=`printf '%08x' $FSRO_END_B | cut -c3-4`
    B3=`printf '%08x' $FSRO_END_B | cut -c5-6`
    B4=`printf '%08x' $FSRO_END_B | cut -c7-8`

    OUTFILE=$TMPPATH/fsro_end_le.bin
    printf '\x'$B4'\x'$B3'\x'$B2'\x'$B1 > $OUTFILE
    /sbin/nandlogical $SMF_MTD WRITE 0x60014, 4, $OUTFILE > /dev/null 2>&1
    /sbin/nandlogical $SMF_MTD WRITE 0x60020, 4, $OUTFILE > /dev/null 2>&1
    /sbin/nandlogical $SMF_MTD WRITE 0x64014, 4, $OUTFILE > /dev/null 2>&1
    /sbin/nandlogical $SMF_MTD WRITE 0x64020, 4, $OUTFILE > /dev/null 2>&1
    rm -f $OUTFILE > /dev/null 2>&1

    printf "%b\n" "$SEP\nPartition  \033[1msmf    root    home\033[0m"
    printf "%b\n" "Size (MB)   \033[1;33m$SMF_SIZE_MB      $FSRO_SIZE_MB      $FSRW_SIZE_MB\033[0m\n$SEP"

    exit 0
}

### Check model ###
/sbin/writerominfo
MODEL=`cat /proc/deviceinfo/product`

case "$MODEL" in
    SL-B500|SL-5600)
        ZAURUS='poodle'
        FLASH_SIZE_MB=64
        DEF_ROOTFS_SIZE_MB=22
        ;;
    SL-C700|SL-C750|SL-C7500)
        ZAURUS='c7x0'
        FLASH_SIZE_MB=64
        DEF_ROOTFS_SIZE_MB=25
        ;;
    SL-C760|SL-C860)
        ZAURUS='c7x0'
        FLASH_SIZE_MB=128
        DEF_ROOTFS_SIZE_MB=53
        ;;
    SL-C1000)
        ZAURUS='akita'
        FLASH_SIZE_MB=128
        DEF_ROOTFS_SIZE_MB=58
        ;;
    SL-C3000)
        ZAURUS='c3x00'
        check_for_hdd
        check_for_tar
        FLASH_SIZE_MB=16
        DEF_ROOTFS_SIZE_MB=5
        ;;
    SL-C3100)
        ZAURUS='c3x00'
        check_for_hdd
        check_for_tar
        FLASH_SIZE_MB=128
        DEF_ROOTFS_SIZE_MB=32
        ;;
    SL-C3200)
        ZAURUS='c3x00'
        check_for_hdd
        check_for_tar
        FLASH_SIZE_MB=128
        DEF_ROOTFS_SIZE_MB=43
        ;;
    SL-6000)
        ZAURUS='tosa'
        FLASH_SIZE_MB=128
        DEF_ROOTFS_SIZE_MB=28
        ;;
    *)
        echo 'MODEL: '$MODEL 'is unsupported'
        echo ''
        echo 'Please reset'
        while true
        do
        done
        ;;
esac

clear
printf "%b\n" "\033[1;32m--- Zaurus Updater ZAURUS_UPDATER_VERSION ---\033[0m\n    MODEL: $MODEL ($ZAURUS)\n"

check_partitions

mkdir -p $TMPPATH > /dev/null 2>&1

cd $DATAPATH/

FOUND_FILES=0
for TARGETFILE in zimage zImage zImage.bin zimage.bin ZIMAGE ZIMAGE.BIN \
                initrd.bin INITRD.BIN hdimage1.tgz HDIMAGE1.TGZ home.bin \
                HOME.BIN mainte_fix.bin MAINTE_FIX.BIN
do
    if [ ! -e $TARGETFILE ]
    then
        continue
    fi
    FOUND_FILES=1

    rm -f $TMPPATH/*.bin > /dev/null 2>&1
    DATASIZE=`wc -c $TARGETFILE`
    DATASIZE=`echo $DATASIZE | cut -d' ' -f1`

    # make TARGETFILE lowercase
    TARGETFILE_LC=`echo $TARGETFILE|tr A-Z a-z`

    case "$TARGETFILE_LC" in

    zimage|zimage.bin)
        if [ $FLASHED_KERNEL != 0 ]
        then
            continue
        fi
        echo 'kernel'
        FLASHED_KERNEL=1
        ISLOGICAL=1
        MODULEID=5
        MTD_PART_SIZE=0x13C000
        ADDR=`dc 0xE0000`
        ISFORMATTED=1
        DATAPOS=0
        ONESIZE=524288
        HDTOP=`expr $DATASIZE - 16`
        /sbin/bcut -a $HDTOP -s 16 -o $TMPHEAD $TARGETFILE
        FLASH_TYPE="kernel"
        do_flashing
        FLASH_TYPE=""
        ;;

    initrd.bin)
        if [ $FLASHED_ROOTFS != 0 ]
        then
            continue
        fi
        echo 'root file system'
        FLASHED_ROOTFS=1
        ISLOGICAL=0
        MODULEID=6
        MTD_PART_SIZE="0x$ROOTFS_SIZE"
        ADDR=0
        ISFORMATTED=0
        TARGET_MTD=$RO_MTD
        DATAPOS=16
        ONESIZE=1048576
        /sbin/bcut -s 16 -o $TMPHEAD $TARGETFILE
        FLASH_TYPE="rootfs"
        do_flashing
        FLASH_TYPE=""
        ;;

    home.bin)
        if [ $FLASHED_HOMEFS != 0 ]
        then
            continue
        fi
        echo 'home file system'
        FLASHED_HOMEFS=1
        ISLOGICAL=0
        ADDR=0
        ISFORMATTED=0
        MTD_PART_SIZE="0x$HOMEFS_SIZE"
        ADDR=0
        TARGET_MTD=$RW_MTD
        #home should not have headers but one could flash a second rootfs here
        DATAPOS=16
        ONESIZE=1048576
        FLASH_TYPE="home"
        /sbin/bcut -s 16 -o $TMPHEAD $TARGETFILE
        do_flashing
        FLASH_TYPE=""
        ;;

    hdimage1.tgz)
        if [ $UNPACKED_ROOTFS = 0 ]
        then
                do_rootfs_extraction
        fi
        ;;

    mainte_fix.bin)
        echo 'Mainte fix for models with 128M flash'
        if [ $FLASHED_MAINTE != 1 ]
        then
                mainte_fix
                FLASHED_MAINTE="1"
        fi
        ;;

    *)
        ;;

    esac
done

if [ $FOUND_FILES = 0 ]
then
    printf "%b\n" "$SEP\n\033[1;33mNo files found to flash!\033[0m\n$SEP\n"
    partition_manager
fi

# reboot
exit 0

# bcut usage: bcut [OPTION] <input file>

# -a: start position
# -s: cut size
# -o: output file

# ModuleId informations used by verchg Sharp binary:
#
# 0 - master
# 1 - Maintaince
# 2 - Diagnostics
# 3 - rescue kernel
# 4 - rescue rootfs
# 5 - normal kernel
# 6 - normal rootfs
# 7 - /home/
# 8 - parameter (whatever it means)
#