@related: arpalhands
#! /bin/bash -e LSUSB_ID="ID 4102:1041 iRiver, Ltd." SECTOR0="eb58904d53444f53352e3000020824000200000000f800003f00ff000000000000107600761d00000000000002000000010006000000000000000000000000000001298acb76f84e4f204e414d4520202020464154333220202033c98ed1bcf47b8ec18ed9bd007c884e028a5640b408cd137305b9ffff8af1660fb6c640660fb6d180e23ff7e286cdc0ed0641660fb7c966f7e1668946f8837e16007538837e2a007732668b461c6683c00cbb0080b90100e82b00e94803a0fa7db47d8bf0ac84c074173cff7409b40ebb0700cd10ebeea0fb7debe5a0f97debe098cd16cd196660663b46f80f824a00666a0066500653666810000100807e02000f852000b441bbaa558a5640cd130f821c0081fb55aa0f851400f6c1010f840d00fe4602b4428a56408bf4cd13b0f96658665866586658eb2a6633d2660fb74e1866f7f1fec28aca668bd066c1ea10f7761a86d68a56408ae8c0e4060accb80102cd1366610f8254ff81c300026640490f8571ffc34e544c4452202020202020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d0a52656d6f7665206469736b73206f72206f74686572206d656469612eff0d0a4469736b206572726f72ff0d0a507265737320616e79206b657920746f20726573746172740d0a0000000000accbd8000055aa" function check_output_file { local output_file="${1}" if [ -f "${output_file}" ]; then echo -n "Overwrite ${output_file} [y/N] ? " read if [ "${REPLY}" != "y" -a "${REPLY}" != "Y" ]; then echo "Abort !" exit fi fi } function create_binary_sector0 { local output_file="${1}" check_output_file "${output_file}" echo -n "${SECTOR0}" | xxd -p -r > "${output_file}" if [ $(wc -c "${output_file}" | awk '{print $1}') = 512 ] ; then echo "${output_file} file creation succeed" else echo "${output_file} file creation failure !" rm -f "${output_file}" exit fi } function check_device_presence { local nb_device="$(lsusb | grep "${LSUSB_ID}" | wc -l)" if [ ${nb_device} = 0 ] ; then echo "iRiver E100 physical device not found !" exit elif [ ${nb_device} -gt 1 ] ; then echo "${nb_device} iRiver E100 physical devices found !" echo "Only one at once is supported !" exit fi } function get_physical_device { check_device_presence local field_set=($(dmesg | cat -n | grep iriver | grep E100 | grep "0: " | tail -1)) if [ ${#field_set[*]} -gt 4 ] ; then NB_LINE="${field_set[0]}" ADDRESS="$(echo "${field_set[4]}" | sed 's/:$//')" else echo "Unable to get physical device !" echo "Not enough number of fields to parse !" exit fi } function get_system_device { get_physical_device local field_set=($(dmesg | grep "${ADDRESS}" | grep "Attached SCSI removable disk")) # TODO: check /proc/bus/scsi if [ ${#field_set[*]} -gt 4 ] ; then DEVICE="$(echo "${field_set[4]}" | tr -d '[]')" else echo "Unable to get system device !" echo "Not enough number of fields to parse !" exit fi } function dump_info { local output_file="${1}" check_output_file "${output_file}" get_physical_device dmesg | sed -n ${NB_LINE},\$p | grep "${ADDRESS}" > "${output_file}" echo "${output_file} file creation succeed" } function read_from_device { local output_file="${1}" check_output_file "${output_file}" get_system_device > "${output_file}" sudo dd if="/dev/${DEVICE}" of="${output_file}" bs=512 count=1 if [ ${?} = 0 -a $(wc -c "${output_file}" | awk '{print $1}') = 512 ] ; then echo "Read sector 0 from iRiver device to ${output_file} file succeed" else echo "Read sector 0 from iRiver device to ${output_file} file failed !" exit fi } function write_to_device { local input_file="${1}" get_system_device sudo dd if="${input_file}" of="/dev/${DEVICE}" bs=512 count=1 if [ ${?} = 0 ] ; then echo "Write sector 0 from ${input_file} file to iRiver device succeed" else echo "Write sector 0 from ${input_file} file to iRiver device failed !" exit fi } function create_archive { local output_file="tmp.tar.gz" [ "$1" != "" ] && output_file="${1}" check_output_file "${output_file}" tar zcf "${output_file}" sector0 dmesg.log if [ ${?} = 0 ] then echo "${output_file} file creation succeed" else echo "${output_file} file creation failure !" exit fi } function create_pkg { read_from_device "sector0" dump_info "dmesg.log" local output_file="${1}" check_output_file "${output_file}" create_archive "${output_file}" } CMD_SET=( rescue read write info package) FUNC_SET=( create_binary_sector0 read_from_device write_to_device dump_info create_pkg) if [ ${#} != 2 ] ; then cat<<EOF Usage: $0 EOF echo -n "[ ${CMD_SET[0]} output_file | ${CMD_SET[1]} output_file | " echo "${CMD_SET[2]} input_file | ${CMD_SET[3]} output_file | ${CMD_SET[4]} output_file ]" echo -e "\t${CMD_SET[0]} : write to output_file a factory 512 bytes sector 0 (USE ONLY TO REPAIR 4 GO IRIVER E100 DEVICE)" echo -e "\t${CMD_SET[1]} : read 512 bytes sector 0 from iRiver E100 device and write them to output_file" echo -e "\t${CMD_SET[2]} : write 512 bytes sector 0 to iRiver E100 device read from input_file " echo -e "\t${CMD_SET[3]} : write to output_file iRiver E100 device dmesg information" echo -e "\t${CMD_SET[4]} : write to output_file a tgz archive containing result of read and info commands" exit fi idx=0 while [ ${idx} -lt ${#CMD_SET[*]} ] ; do if [ "${1}" = "${CMD_SET[idx]}" ] then ${FUNC_SET[idx]} "${2}" exit fi ((++idx)) done echo "${1} is an unknown command !"