Daily Archives: 2017-08-10

Write Bin File by Bash & Awk

Sometimes I find I am locked out VoCore2 wifi, because some executable files missed or messed up the factory setting. The only exists connection is the uart console.

PS: if you messed up factory setting, the wireless driver will not work anymore, because the driver read factory setting for some important data such as mac address, timer offset etc…

I might have three choices:

1. connect a cable to ethernet port — but I do not have…
2. on vocore.io/v2, fix brick part, use another VoCore2 SPI reflash the flash — but I only have the broken one…
3. use iwpriv, manually set the driver — around 30+ settings, I am sure I do not want to do that…

so I am looking for a way using bash and awk to write a bin file, even it is slow, but no problem, I accept, at least it is fast than the upper ways.:)

We will need three commands, bash/awk/dd

Key to make this happen:
awk can do most of the work, but it can not write zero to a file, we will need use dd copy a zero from /dev/zero.

Code:

#!/bin/bash

create_byte_map() {
dd if=/dev/zero of=/tmp/map bs=1 count=1 2> /dev/null
echo | awk '{ for(i = 0; i < 256; i++) { printf("%c", i); }}' >> /tmp/map
}

attach_byte() {
name=$1; byte=$2;
if [ -f $name ]; then size=`ls -l ${name} | awk '{print $5}'`; else size=0; fi
dd if=/tmp/map of=${name} bs=1 count=1 seek=${size} skip=${byte} 2> /dev/null
}

attach_bytes() {
name=$1; string=$2;
echo $string | awk -F " " '{for (i = 1; i <= NF; i++) print $i}' | while read byte
do attach_byte $name $byte; done
}

create_byte_map;

# example:
attach_bytes /tmp/myfile "`echo $1 | awk '{printf("184 216 18 103 0 88");}'`";
hexdump -C /tmp/myfile

If I want output some char(0~255) to a file, just call the bash function:
attach_bytes [output file] [char in decimal(0~255)]

This looks like stupid but sometimes it really helpful. If there is any clever way, let me know 🙂