~aleteoryx/muditaos

5c574c1edc630dedcd02f6ef6d5bd42b1b2303e0 — Lucjan Bryndza 4 years ago c51411c
[EGD-5877] Remove flash script update flasing doc

Remove old flashEMMC script and update flash documentation

Update doc/boot_and_update.md

Co-authored-by: Wojtek Cichoń <wojtek.cichon@protonmail.com>

Update doc/running_on_phone.md

Co-authored-by: Wojtek Cichoń <wojtek.cichon@protonmail.com>

Update doc/running_on_phone.md

Co-authored-by: Wojtek Cichoń <wojtek.cichon@protonmail.com>

Update doc/running_on_phone.md

Co-authored-by: Wojtek Cichoń <wojtek.cichon@protonmail.com>

Update doc/boot_and_update.md

Co-authored-by: Wojtek Cichoń <wojtek.cichon@protonmail.com>

Update doc/running_on_phone.md

Co-authored-by: Wojtek Cichoń <wojtek.cichon@protonmail.com>
3 files changed, 37 insertions(+), 309 deletions(-)

M doc/boot_and_update.md
M doc/running_on_phone.md
D flash_eMMC.sh
M doc/boot_and_update.md => doc/boot_and_update.md +30 -183
@@ 62,219 62,66 @@ The type variable in `boot.json` is for MuditaOS - this will indicate a subdirec
# sudo umount /mnt 
```

6. Move into the project root directory and run the partitioning script, but:
6. Move into the project root directory and run the image generation script

```bash
$ sudo ./config/partition_emmc.sh /dev/sdX -f
    ./generate_purephone_image.sh pure.img build-rt1051-Debug
```
**Note:** Replace `/dev/sdX` with the disk name you found for your phone.
   
   Example output:
```
[atom@nucleus:~/devel/PurePhone_10]$ sudo ./config/partition_emmc.sh /dev/sda -f
/dev/sda is not mounted
/dev/sda has 2 partitions
Checking that no-one is using this disk right now ... OK

Disk /dev/sda: 14,62 GiB, 15678308352 bytes, 30621696 sectors
Disk model: NXP MASS STORAGE
Disk /home/lucck/worksrc/MuditaOS/pure.img: 14,62 GiB, 15678308352 bytes, 30621696 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x09650eb4

Old situation:

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1  *        2048 28524543 28522496 13,6G  b W95 FAT32
/dev/sda2       28524544 30621695  2097152    1G  b W95 FAT32

>>> Script header accepted.
>>> Script header accepted.
>>> Script header accepted.
>>> Script header accepted.
>>> Created a new DOS disklabel with disk identifier 0x09650eb4.
/dev/sda1: Created a new partition 1 of type 'W95 FAT32' and of size 13,6 GiB.
Partition #1 contains a vfat signature.
/dev/sda2: Created a new partition 2 of type 'W95 FAT32' and of size 1 GiB.
Partition #2 contains a vfat signature.
/dev/sda3: Done.
/home/lucck/worksrc/MuditaOS/pure.img1: Created a new partition 1 of type 'W95 FAT32' and of size 1 GiB.
/home/lucck/worksrc/MuditaOS/pure.img2: Created a new partition 2 of type 'W95 FAT32' and of size 1 GiB.
/home/lucck/worksrc/MuditaOS/pure.img3: Created a new partition 3 of type 'Unknown' and of size 12,6 GiB.
/home/lucck/worksrc/MuditaOS/pure.img4: Done.

New situation:
Disklabel type: dos
Disk identifier: 0x09650eb4

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1  *        2048 28524543 28522496 13,6G  b W95 FAT32
/dev/sda2       28524544 30621695  2097152    1G  b W95 FAT32
Device                                 Boot   Start      End  Sectors  Size Id Type
/home/lucck/worksrc/MuditaOS/pure.img1 *       2048  2099199  2097152    1G  b W95 FAT32
/home/lucck/worksrc/MuditaOS/pure.img2      2099200  4196351  2097152    1G  b W95 FAT32
/home/lucck/worksrc/MuditaOS/pure.img3      4196352 30621695 26425344 12,6G 9e unknown

The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
create FATs
FAT: PUREOS /dev/sda1
mkfs.fat 4.1 (2017-01-24)
FAT: RECOVER /dev/sda2
mkfs.fat 4.1 (2017-01-24)
probe new partitions to OS
Littlefs summary:
     Directories created: 5, Files added: 24, Transferred 1198 kbytes.
          Littlefs block size: 32768 blocks: 57/412895.
          /home/lucck/worksrc/MuditaOS

```

7. Once completed, flash the phone with the latest MuditaOS build. To do that
mount the first partition of the phone to any location (ex. `/mnt`): `$ sudo mount /dev/sdX1 /mnt`
7. Once completed, flash the phone with the latest MuditaOS build. To do that run the `dd` command 
on the image:

**Note:** Replace `/dev/sdX1` with the disk name on your phone.
```bash
sudo dd if=pure.img of=/dev/sdX status=progress  conv=sparse 
```

8. Run the flash script (assuming you built your MuditaOS in the build-rt1051-Debug folder)
**Note:** Replace `/dev/sdX` with the disk name you found for your phone after connecting it to your computer.

Example output:

```bash
[atom@nucleus:~/devel/PurePhone_10]$ sudo ./flash_eMMC.sh ./build-rt1051-Debug
PurePhone remove all files
Create directories
PurePhone copy build files
'./build-rt1051-Debug/boot.bin' -> '/mnt/current/boot.bin'
Copying assets
'./build-rt1051-Debug/assets' -> '/mnt/current/assets'
'./build-rt1051-Debug/assets/profiles' -> '/mnt/current/assets/profiles'
'./build-rt1051-Debug/assets/profiles/template.kprof' -> '/mnt/current/assets/profiles/template.kprof'
'./build-rt1051-Debug/assets/profiles/phone.kprof' -> '/mnt/current/assets/profiles/phone.kprof'
'./build-rt1051-Debug/assets/profiles/lang_sp_upper.kprof' -> '/mnt/current/assets/profiles/lang_sp_upper.kprof'
'./build-rt1051-Debug/assets/profiles/lang_sp_lower.kprof' -> '/mnt/current/assets/profiles/lang_sp_lower.kprof'
'./build-rt1051-Debug/assets/profiles/lang_pl_upper.kprof' -> '/mnt/current/assets/profiles/lang_pl_upper.kprof'
'./build-rt1051-Debug/assets/profiles/lang_pl_lower.kprof' -> '/mnt/current/assets/profiles/lang_pl_lower.kprof'
'./build-rt1051-Debug/assets/profiles/lang_eng_upper.kprof' -> '/mnt/current/assets/profiles/lang_eng_upper.kprof'
'./build-rt1051-Debug/assets/profiles/lang_de_upper.kprof' -> '/mnt/current/assets/profiles/lang_de_upper.kprof'
'./build-rt1051-Debug/assets/profiles/numeric.kprof' -> '/mnt/current/assets/profiles/numeric.kprof'
'./build-rt1051-Debug/assets/profiles/lang_eng_lower.kprof' -> '/mnt/current/assets/profiles/lang_eng_lower.kprof'
'./build-rt1051-Debug/assets/profiles/lang_de_lower.kprof' -> '/mnt/current/assets/profiles/lang_de_lower.kprof'
'./build-rt1051-Debug/assets/images' -> '/mnt/current/assets/images'
'./build-rt1051-Debug/assets/images/phonebook_search_glass_grey.vpi' -> '/mnt/current/assets/images/phonebook_search_glass_grey.vpi'
'./build-rt1051-Debug/assets/images/menu_calendar_W_G.vpi' -> '/mnt/current/assets/images/menu_calendar_W_G.vpi'
'./build-rt1051-Debug/assets/images/menu_contacts_W_G.vpi' -> '/mnt/current/assets/images/menu_contacts_W_G.vpi'
'./build-rt1051-Debug/assets/images/mail.vpi' -> '/mnt/current/assets/images/mail.vpi'
'./build-rt1051-Debug/assets/images/logo.vpi' -> '/mnt/current/assets/images/logo.vpi'
'./build-rt1051-Debug/assets/images/left_label_arrow.vpi' -> '/mnt/current/assets/images/left_label_arrow.vpi'
'./build-rt1051-Debug/assets/images/signal1.vpi' -> '/mnt/current/assets/images/signal1.vpi'
'./build-rt1051-Debug/assets/images/enter_icon_alpha_W_M.vpi' -> '/mnt/current/assets/images/enter_icon_alpha_W_M.vpi'
'./build-rt1051-Debug/assets/images/calllog_arrow_out.mpi' -> '/mnt/current/assets/images/calllog_arrow_out.mpi'
'./build-rt1051-Debug/assets/images/calllog_arrow_in.mpi' -> '/mnt/current/assets/images/calllog_arrow_in.mpi'
'./build-rt1051-Debug/assets/images/messages_error_W_M.vpi' -> '/mnt/current/assets/images/messages_error_W_M.vpi'
'./build-rt1051-Debug/assets/images/mute.vpi' -> '/mnt/current/assets/images/mute.vpi'
'./build-rt1051-Debug/assets/images/homescreen-locked.vpi' -> '/mnt/current/assets/images/homescreen-locked.vpi'
'./build-rt1051-Debug/assets/images/battery1.vpi' -> '/mnt/current/assets/images/battery1.vpi'
'./build-rt1051-Debug/assets/images/battery5.vpi' -> '/mnt/current/assets/images/battery5.vpi'
'./build-rt1051-Debug/assets/images/battery4.vpi' -> '/mnt/current/assets/images/battery4.vpi'
'./build-rt1051-Debug/assets/images/battery3.vpi' -> '/mnt/current/assets/images/battery3.vpi'
'./build-rt1051-Debug/assets/images/lock.vpi' -> '/mnt/current/assets/images/lock.vpi'
'./build-rt1051-Debug/assets/images/dot_12px_hard_alpha_W_G.vpi' -> '/mnt/current/assets/images/dot_12px_hard_alpha_W_G.vpi'
'./build-rt1051-Debug/assets/images/battery2.vpi' -> '/mnt/current/assets/images/battery2.vpi'
'./build-rt1051-Debug/assets/images/signal5.vpi' -> '/mnt/current/assets/images/signal5.vpi'
'./build-rt1051-Debug/assets/images/small_circle.vpi' -> '/mnt/current/assets/images/small_circle.vpi'
'./build-rt1051-Debug/assets/images/menu_settings_W_G.vpi' -> '/mnt/current/assets/images/menu_settings_W_G.vpi'
'./build-rt1051-Debug/assets/images/simunknown.vpi' -> '/mnt/current/assets/images/simunknown.vpi'
'./build-rt1051-Debug/assets/images/speaker.vpi' -> '/mnt/current/assets/images/speaker.vpi'
'./build-rt1051-Debug/assets/images/menu_meditation_W_G.vpi' -> '/mnt/current/assets/images/menu_meditation_W_G.vpi'
'./build-rt1051-Debug/assets/images/circle_bottom.mpi' -> '/mnt/current/assets/images/circle_bottom.mpi'
'./build-rt1051-Debug/assets/images/phonebook_empty_grey_circle_W_G.vpi' -> '/mnt/current/assets/images/phonebook_empty_grey_circle_W_G.vpi'
'./build-rt1051-Debug/assets/images/calllog_arrow_den.mpi' -> '/mnt/current/assets/images/calllog_arrow_den.mpi'
'./build-rt1051-Debug/assets/images/info_big_circle_W_G.vpi' -> '/mnt/current/assets/images/info_big_circle_W_G.vpi'
'./build-rt1051-Debug/assets/images/dot_12px_hard_alpha_W_M.vpi' -> '/mnt/current/assets/images/dot_12px_hard_alpha_W_M.vpi'
'./build-rt1051-Debug/assets/images/menu_phone_W_G.vpi' -> '/mnt/current/assets/images/menu_phone_W_G.vpi'
'./build-rt1051-Debug/assets/images/menu_tools_calculator_W_G.vpi' -> '/mnt/current/assets/images/menu_tools_calculator_W_G.vpi'
'./build-rt1051-Debug/assets/images/signal0.vpi' -> '/mnt/current/assets/images/signal0.vpi'
'./build-rt1051-Debug/assets/images/menu_alarm_W_G.vpi' -> '/mnt/current/assets/images/menu_alarm_W_G.vpi'
'./build-rt1051-Debug/assets/images/arrow_left.vpi' -> '/mnt/current/assets/images/arrow_left.vpi'
'./build-rt1051-Debug/assets/images/menu_messages_W_G.vpi' -> '/mnt/current/assets/images/menu_messages_W_G.vpi'
'./build-rt1051-Debug/assets/images/battery0.vpi' -> '/mnt/current/assets/images/battery0.vpi'
'./build-rt1051-Debug/assets/images/menu_music_player_W_G.vpi' -> '/mnt/current/assets/images/menu_music_player_W_G.vpi'
'./build-rt1051-Debug/assets/images/menu_tools_W_G.vpi' -> '/mnt/current/assets/images/menu_tools_W_G.vpi'
'./build-rt1051-Debug/assets/images/right_label_arrow.vpi' -> '/mnt/current/assets/images/right_label_arrow.vpi'
'./build-rt1051-Debug/assets/images/menu_tools_recorder_W_G.vpi' -> '/mnt/current/assets/images/menu_tools_recorder_W_G.vpi'
'./build-rt1051-Debug/assets/images/microphone_off.vpi' -> '/mnt/current/assets/images/microphone_off.vpi'
'./build-rt1051-Debug/assets/images/pin_lock_info.vpi' -> '/mnt/current/assets/images/pin_lock_info.vpi'
'./build-rt1051-Debug/assets/images/microphone_on.vpi' -> '/mnt/current/assets/images/microphone_on.vpi'
'./build-rt1051-Debug/assets/images/cross.vpi' -> '/mnt/current/assets/images/cross.vpi'
'./build-rt1051-Debug/assets/images/phone.vpi' -> '/mnt/current/assets/images/phone.vpi'
'./build-rt1051-Debug/assets/images/phonebook_search_arrow_grey.vpi' -> '/mnt/current/assets/images/phonebook_search_arrow_grey.vpi'
'./build-rt1051-Debug/assets/images/phonebook_blocked_large.mpi' -> '/mnt/current/assets/images/phonebook_blocked_large.mpi'
'./build-rt1051-Debug/assets/images/speaker_on.vpi' -> '/mnt/current/assets/images/speaker_on.vpi'
'./build-rt1051-Debug/assets/images/phonebook_blocked_large.vpi' -> '/mnt/current/assets/images/phonebook_blocked_large.vpi'
'./build-rt1051-Debug/assets/images/phonebook_contact_delete_trashcan.vpi' -> '/mnt/current/assets/images/phonebook_contact_delete_trashcan.vpi'
'./build-rt1051-Debug/assets/images/phonebook_info.vpi' -> '/mnt/current/assets/images/phonebook_info.vpi'
'./build-rt1051-Debug/assets/images/search_big.mpi' -> '/mnt/current/assets/images/search_big.mpi'
'./build-rt1051-Debug/assets/images/phonebook_phone_ringing.vpi' -> '/mnt/current/assets/images/phonebook_phone_ringing.vpi'
'./build-rt1051-Debug/assets/images/circle_top.mpi' -> '/mnt/current/assets/images/circle_top.mpi'
'./build-rt1051-Debug/assets/images/pin_lock.vpi' -> '/mnt/current/assets/images/pin_lock.vpi'
'./build-rt1051-Debug/assets/images/phonebook_small.vpi' -> '/mnt/current/assets/images/phonebook_small.vpi'
'./build-rt1051-Debug/assets/images/search.vpi' -> '/mnt/current/assets/images/search.vpi'
'./build-rt1051-Debug/assets/images/signal2.vpi' -> '/mnt/current/assets/images/signal2.vpi'
'./build-rt1051-Debug/assets/images/signal3.vpi' -> '/mnt/current/assets/images/signal3.vpi'
'./build-rt1051-Debug/assets/images/signal4.vpi' -> '/mnt/current/assets/images/signal4.vpi'
'./build-rt1051-Debug/assets/images/phonebook_search_arrow_down_grey.vpi' -> '/mnt/current/assets/images/phonebook_search_arrow_down_grey.vpi'
'./build-rt1051-Debug/assets/images/sim2.vpi' -> '/mnt/current/assets/images/sim2.vpi'
'./build-rt1051-Debug/assets/images/sim1.vpi' -> '/mnt/current/assets/images/sim1.vpi'
'./build-rt1051-Debug/assets/images/arrow_right.vpi' -> '/mnt/current/assets/images/arrow_right.vpi'
'./build-rt1051-Debug/assets/images/simfail.vpi' -> '/mnt/current/assets/images/simfail.vpi'
'./build-rt1051-Debug/assets/images/small_heart.vpi' -> '/mnt/current/assets/images/small_heart.vpi'
'./build-rt1051-Debug/assets/images/small_tick.vpi' -> '/mnt/current/assets/images/small_tick.vpi'
'./build-rt1051-Debug/assets/images/call_message.vpi' -> '/mnt/current/assets/images/call_message.vpi'
'./build-rt1051-Debug/assets/images/menu_tools_notes_W_G.vpi' -> '/mnt/current/assets/images/menu_tools_notes_W_G.vpi'
'./build-rt1051-Debug/assets/images/homescreen_lock.mpi' -> '/mnt/current/assets/images/homescreen_lock.mpi'
'./build-rt1051-Debug/assets/images/speaker_off.vpi' -> '/mnt/current/assets/images/speaker_off.vpi'
'./build-rt1051-Debug/assets/fonts' -> '/mnt/current/assets/fonts'
'./build-rt1051-Debug/assets/fonts/gt_pressura_regular_30.mpf' -> '/mnt/current/assets/fonts/gt_pressura_regular_30.mpf'
'./build-rt1051-Debug/assets/fonts/gt_pressura_light_30.mpf' -> '/mnt/current/assets/fonts/gt_pressura_light_30.mpf'
'./build-rt1051-Debug/assets/fonts/gt_pressura_light_27.mpf' -> '/mnt/current/assets/fonts/gt_pressura_light_27.mpf'
'./build-rt1051-Debug/assets/fonts/gt_pressura_bold_30.mpf' -> '/mnt/current/assets/fonts/gt_pressura_bold_30.mpf'
'./build-rt1051-Debug/assets/fonts/gt_pressura_bold_27.mpf' -> '/mnt/current/assets/fonts/gt_pressura_bold_27.mpf'
'./build-rt1051-Debug/assets/fonts/gt_pressura_bold_32.mpf' -> '/mnt/current/assets/fonts/gt_pressura_bold_32.mpf'
'./build-rt1051-Debug/assets/fonts/gt_pressura_bold_20.mpf' -> '/mnt/current/assets/fonts/gt_pressura_bold_20.mpf'
'./build-rt1051-Debug/assets/fonts/gt_pressura_regular_20.mpf' -> '/mnt/current/assets/fonts/gt_pressura_regular_20.mpf'
'./build-rt1051-Debug/assets/fonts/gt_pressura_regular_27.mpf' -> '/mnt/current/assets/fonts/gt_pressura_regular_27.mpf'
'./build-rt1051-Debug/assets/fonts/gt_pressura_regular_24.mpf' -> '/mnt/current/assets/fonts/gt_pressura_regular_24.mpf'
'./build-rt1051-Debug/assets/fonts/gt_pressura_bold_24.mpf' -> '/mnt/current/assets/fonts/gt_pressura_bold_24.mpf'
'./build-rt1051-Debug/assets/fonts/gt_pressura_light_90.mpf' -> '/mnt/current/assets/fonts/gt_pressura_light_90.mpf'
'./build-rt1051-Debug/assets/fonts/gt_pressura_light_46.mpf' -> '/mnt/current/assets/fonts/gt_pressura_light_46.mpf'
'./build-rt1051-Debug/assets/lang' -> '/mnt/current/assets/lang'
'./build-rt1051-Debug/assets/lang/lang_en.json' -> '/mnt/current/assets/lang/lang_en.json'
'./build-rt1051-Debug/assets/lang/lang_sp.json' -> '/mnt/current/assets/lang/lang_sp.json'
'./build-rt1051-Debug/assets/lang/lang_de.json' -> '/mnt/current/assets/lang/lang_de.json'
'./build-rt1051-Debug/assets/lang/lang_pl.json' -> '/mnt/current/assets/lang/lang_pl.json'
'./build-rt1051-Debug/assets/audio' -> '/mnt/current/assets/audio'
'./build-rt1051-Debug/assets/audio/ringtone.wav' -> '/mnt/current/assets/audio/ringtone.wav'
'./build-rt1051-Debug/assets/audio/alarm_clock.mp3' -> '/mnt/current/assets/audio/alarm_clock.mp3'
Copying country-codes.db
'./build-rt1051-Debug/country-codes.db' -> '/mnt/current/country-codes.db'
Copying Luts.bin
'./build-rt1051-Debug/Luts.bin' -> '/mnt/current/Luts.bin'
Copying user
'./build-rt1051-Debug/user' -> '/mnt/current/user'
'./build-rt1051-Debug/user/settings.db' -> '/mnt/current/user/settings.db'
'./build-rt1051-Debug/user/calllog.db' -> '/mnt/current/user/calllog.db'
'./build-rt1051-Debug/user/contacts.db' -> '/mnt/current/user/contacts.db'
'./build-rt1051-Debug/user/sms.db' -> '/mnt/current/user/sms.db'
'./build-rt1051-Debug/user/notes.db' -> '/mnt/current/user/notes.db'
Copying .boot.ini
'./build-rt1051-Debug/.boot.ini' -> '/mnt/current/.boot.ini'
Copying .boot.ini.crc32
'./build-rt1051-Debug/.boot.ini.crc32' -> '/mnt/current/.boot.ini.crc32'
Moving /mnt/current/user to /mnt
Moving /mnt/current/.boot.ini to /mnt
Moving /mnt/current/.boot.ini.crc32 to /mnt
PurePhone copied

PurePhone unmouted
PurePhone ejected
Done. You can reset PurePhone now

sudo dd if=pure.img of=/dev/sda status=progress  conv=sparse                    
[sudo] password for lucck: 
Sorry, try again.
[sudo] password for lucck: 
15484559360 bytes (15 GB, 14 GiB) copied, 19 s, 815 MB/s 
30621696+0 records in
30621696+0 records out
15678308352 bytes (16 GB, 15 GiB) copied, 19,3531 s, 810 MB/s
```

9. In case all of the above fails, try using [gparted](https://gparted.org/) on the destination device. You should see two FAT partitions on a single disk. If that's not the case, use `gparted` to create a new partition table and re-run the partition script.
   
This is how gparted should look like if the disk is OK
   ![Gparted OK](./Images/gparted_ok.png "workflow")
   
This is a case if the disk is not correct
   ![Gparted OK](./Images/gparted_fail.png "workflow")

M doc/running_on_phone.md => doc/running_on_phone.md +7 -4
@@ 33,10 33,10 @@ Files required to boot from eMMC are:
- firmware image checksum files: `.boot.json` and `.boot.json.crc32`
- assets (images, fonts, sounds, databases)

### Upload to eMMC
### Generating image and uploading it to the eMMC

- [Mount Mudita Pure as a USB MSC (disk drive)](#How-to-mount-Mudita-Pure-as-a-USB-MSC)
- Use convenience script `flash_eMMC.sh <build_folder; default="build">` to copy both the executable **AND** the assets. **Note:** databases will be restored to their default. This script copies everything needed for the phone to work. This means that after running it you can remove `boot.bin` from the phone, load the code to SDRAM and work on current assets.
-  Use convenience script `generate_purephone_image.sh <image_file.img> <build_folder>` to generate Mudita Pure burning image.  
-  Use the `dd` command to burn image into the eMMC card `sudo dd if=<image_file.img> of=/dev/sdX status=progress conv=sparse` **Note:** Replace `/dev/sdX` with the disk name you found for your phone after connecting it to your computer.

## How to mount Mudita Pure as a USB MSC



@@ 50,4 50,7 @@ If you have a bootloader flashed and working use the following key combination, 
- briefly press reset button (the one closest to the phone on the green breakout board)
- wait while text scrolls from the bottom
- release centre button
- press key 4
\ No newline at end of file
- press key 4

FAT partition can be mounted directly by the Linux kernel. LittleFS partition can be mounted via
FUSE using the following command `lfsfuse /dev/sdX <mount_point> --block_size=32768`

D flash_eMMC.sh => flash_eMMC.sh +0 -122
@@ 1,122 0,0 @@
#!/bin/bash
# Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
# For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

# set -e

source config/common.sh

if [ -z $1 ]; then
  BUILD_PATH=./build-rt1051-Debug
  echo "copying from default: $BUILD_PATH"
  echo -e "usage: $0 <build-folder>\n"
else
  BUILD_PATH=$1
fi

if [ ! -d $BUILD_PATH ]; then
	echo "build directory: $BUILD_PATH does not exit"
	exit 2
fi

check_target_rt1051 "$BUILD_PATH"

MUDITAOS_DEV=/dev/disk/by-id/usb-NXP_SEMI_NXP_MASS_STORAGE_0123456789ABCDEF-0:0-part1
MUDITAOS_DEV_RECOVER=/dev/disk/by-id/usb-NXP_SEMI_NXP_MASS_STORAGE_0123456789ABCDEF-0:0-part2
MUDITAOS_DISK=`readlink -f $MUDITAOS_DEV`
MUDITAOS_DISK_RECOVER=`readlink -f $MUDITAOS_DEV_RECOVER`
# check if already mouted

if [ ! -f $BUILD_PATH/boot.bin ]; then
	echo
	echo "boot.bin does not exist in $BUILD_PATH, maybe you need to $ make in this dir"
	echo
	exit 2
fi

MOUNT_ENTRY_RECOVER=$(grep "$MUDITAOS_DISK_RECOVER" /etc/mtab)
MOUNT_ENTRY=$(grep "$MUDITAOS_DISK" /etc/mtab)
if [ $? -ne 0 ]; then
	>&2 echo "PurePhone not mounted. Mount or retry with Ozone. https://github.com/muditacom/PurePhone/blob/master/doc/running_on_phone.md#eMMC_upload"
	exit 2
fi
grep "[[:space:]]ro[[:space:],]" <<< $MOUNT_ENTRY
if [ $? -eq 0 ]; then
	>&2 echo "PurePhone mounted in readonly mode please unmount fs and check disc with command"
	>&2 echo "sudo fsck.vfat -a $(awk '{ print $1 }' <<< $MOUNT_ENTRY)"
	exit 3
fi
grep "[[:space:]]ro[[:space:],]" <<< $MOUNT_ENTRY_RECOVER
if [ $? -eq 0 ]; then
	>&2 echo "PurePhone mounted in readonly mode please unmount fs and check disc with command"
	>&2 echo "sudo fsck.vfat -a $(awk '{ print $1 }' <<< $MOUNT_ENTRY_RECOVER)"
	exit 4
fi

MUDITAOS_PATH=$(echo $MOUNT_ENTRY | awk -F " " '{print $2}')
# unespace
MUDITAOS_PATH_CURRENT=$(echo -e $MUDITAOS_PATH)/current
MUDITAOS_PATH_ROOT=$(echo -e $MUDITAOS_PATH)

echo "PurePhone remove all files"
rm -rf "$MUDITAOS_PATH_ROOT"/* -r &>/dev/null || echo "PurePhone disk alread empty"
rm -f "$MUDITAOS_PATH_ROOT"/.boot.json
rm -f "$MUDITAOS_PATH_ROOT"/.boot.json.crc32
rm -rf "$MUDITAOS_PATH_ROOT"/.Trash-1000

if [ "$(ls -A $MUDITAOS_PATH_ROOT)" ]; then
	echo "Couldn't delete all files in $MUDITAOS_PATH_ROOT leftover files:"
	find $MUDITAOS_PATH_ROOT -type f -printf "%f\n"
	exit 5
else
	echo "$MUDITAOS_PATH_ROOT is Empty"
fi

echo "Create directories"
mkdir -p $MUDITAOS_PATH_ROOT/$MUDITAOS_CURRENT
mkdir -p $MUDITAOS_PATH_ROOT/$MUDITAOS_PREVIOUS
mkdir -p $MUDITAOS_PATH_ROOT/$MUDITAOS_FACTORY
mkdir -p $MUDITAOS_PATH_ROOT/$MUDITAOS_UPDATES

# sudo sync $MUDITAOS_DEV

echo "PurePhone copy build files"
cp -v $BUILD_PATH/boot.bin "$MUDITAOS_PATH_CURRENT"/  # | sed 's/'-\>'/'→'/g'

for file in $IMAGE_FILES; do
	echo Copying $file
	cp -vr $BUILD_PATH/$file "$MUDITAOS_PATH_CURRENT"/
done

for file in $IMAGE_MOVE_TO_ROOT; do
	echo "Moving $MUDITAOS_PATH_CURRENT/$file to $MUDITAOS_PATH_ROOT"
	mv "$MUDITAOS_PATH_CURRENT"/"$file" "$MUDITAOS_PATH_ROOT"/
done

# sudo sync $MUDITAOS_DEV # https://unix.stackexchange.com/a/345950
echo -e "PurePhone copied\n"

MUDITAOS_PARTITION=$(lsblk -nlp $MUDITAOS_DISK | tail +2 | awk '{print $1}')

if [ -z $MUDITAOS_PARTITION ]; then
       MUDITAOS_PARTITION=$MUDITAOS_DISK # it is formatted like so apparently
fi

echo "Syncing data"
sync && sync && sync

if [ "$MOUNT_ENTRY" != "" ]; then
	echo "Unmounting disk: $MUDITAOS_DISK ($MOUNT_ENTRY)"
	udisksctl unmount -b $MUDITAOS_DISK > /dev/null
fi

if [ "$MOUNT_ENTRY_RECOVER" != "" ]; then
	echo "Unmounting disk: $MUDITAOS_DISK_RECOVER ($MOUNT_ENTRY_RECOVER)"
	udisksctl unmount -b $MUDITAOS_DISK_RECOVER > /dev/null
fi

sleep 1
echo "PurePhone unmouted"
echo "Ejecting USB"
timeout --signal=SIGINT 1 udisksctl power-off -b $MUDITAOS_DISK
echo "Reset your phone now"