VIDPID substitution at the last stage of build.
authorNIIBE Yutaka <gniibe@fsij.org>
Fri, 19 Jan 2018 06:40:46 +0000 (15:40 +0900)
committerNIIBE Yutaka <gniibe@fsij.org>
Fri, 19 Jan 2018 06:40:46 +0000 (15:40 +0900)
.gitignore
ChangeLog
NEUG_USB_DEVICE_ID
chopstx
src/Makefile
src/binary-edit.sh [new file with mode: 0644]
src/configure
src/main.c

index bb98863..c954761 100644 (file)
@@ -9,3 +9,4 @@ src/config.h
 src/config.mk
 src/disk-on-rom.h
 src/neug.ld
+src/put-vid-pid-ver.sh
index 88084d2..571f453 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2018-01-19  NIIBE Yutaka  <gniibe@fsij.org>
+
+       * src/binary-edit.sh: New.
+       * src/configure (output_vid_pid_version): Generate a shell script.
+       * src/Makefile (build/neug-vidpid.elf): New target.
+       * src/main.c (vcom_device_desc): Make this array as a template.
+
+       * NEUG_USB_DEVICE_ID (bcdDev): Bump to 2.00.
+
+       * chopstx: Update to 1.8.
+
 2018-01-18  NIIBE Yutaka  <gniibe@fsij.org>
 
        * src/main.c (usb_main): USB suspend fix for Fraucheky.
index 1c5717c..ee82354 100644 (file)
@@ -1,5 +1,5 @@
 # VID:PID      bcdDev  Product_STRING  Vender_STRING
-234b:0004      0100    Fraucheky       Free Software Initiative of Japan
-234b:0001      0100    NeuG True RNG   Free Software Initiative of Japan
+234b:0004      0200    Fraucheky       Free Software Initiative of Japan
+234b:0001      0200    NeuG True RNG   Free Software Initiative of Japan
 0000:0000      0200    NeuG Emulation  Free Software Initiative of Japan
 ##########<TAB>        ##<TAB> ##########<TAB> #################
diff --git a/chopstx b/chopstx
index 74cff25..aa63ac7 160000 (submodule)
--- a/chopstx
+++ b/chopstx
@@ -1 +1 @@
-Subproject commit 74cff25b78aa293562595fb287a498d5150ac2f8
+Subproject commit aa63ac79bc331b7c60943d77ec3c1f82e8c353d9
index 5f179dc..cd5716a 100644 (file)
@@ -53,7 +53,13 @@ distclean:: clean
        -rm -f neug.ld config.h board.h config.mk \
               usb-strings.c.inc usb-vid-pid-ver.c.inc
 
-ifneq ($(EMULATION),)
+ifeq ($(EMULATION),)
+build/neug-vidpid.elf: build/neug.elf binary-edit.sh put-vid-pid-ver.sh
+       cp -p build/neug.elf build/neug-vidpid.elf
+       bash put-vid-pid-ver.sh
+       $(OBJCOPY) -O ihex build/neug-vidpid.elf build/neug-vidpid.hex
+       $(OBJCOPY) -O binary build/neug-vidpid.elf build/neug-vidpid.bin
+else
 # By specifying DESTDIR on invocation of "make", you can install
 # program to different ROOT.
 
diff --git a/src/binary-edit.sh b/src/binary-edit.sh
new file mode 100644 (file)
index 0000000..26acba0
--- /dev/null
@@ -0,0 +1,79 @@
+# This is a Bash script to be included.
+
+FILE="build/neug-vidpid.elf"
+
+# Idx Name          Size      VMA       LMA       File off  Algn
+# =================
+#   2 .text         00004a40  080010f0  080010f0  000110f0  2**4
+# 08006500 l     O .text       00000012 vcom_device_desc
+# 08006550 l     O .text       00000012 device_desc
+# =================
+# VMA =0x080010f0
+# FOFF=0x000110f0
+# ADDR=0x08005ad0
+# file_off_ADDR = ADDR - VMA + FOFF
+#               = 0x08005ad0 - 0x080010f0 + 0x000110f0 = 0x00015ad0
+
+function calc_addr () {
+    local line_sym="" VMA FOFF ADDR
+
+    arm-none-eabi-objdump -h -t -j .text $FILE       | \
+    egrep -e '(^ +[0-9] +\.text +|device_desc)' | \
+    while read -r F0 F1 F2 F3 F4 F5 F6; do
+       if [ -z "$line_sym" ]; then
+           VMA=$F3
+           FOFF=$F5
+           line_sym="next is a line for the symbol"
+       else
+           ADDR=$F0
+           echo "$((0x$ADDR - 0x$VMA + 0x$FOFF))"
+       fi
+    done
+}
+
+declare -a OFFSETS
+OFFSETS=($(calc_addr))
+file_off_ADDR=${OFFSETS[0]}
+file_off_fraucheky_ADDR=${OFFSETS[1]}
+
+echo "Offset is $file_off_ADDR"
+if [ -n "$file_off_fraucheky_ADDR" ]; then
+    echo "Offset is $file_off_fraucheky_ADDR"
+fi
+
+function replace_file_byte_at () {
+    printf "\x$1" | dd of=$FILE bs=1 seek=$2 conv=notrunc >& /dev/null
+}
+
+#
+# vid_lsb:         8
+# vid_msb:         9
+# pid_lsb:        10
+# pid_msb:        11
+# bcd_device_lsb: 12
+# bcd_device_msb: 13
+#
+
+function replace_vid_lsb () {
+    replace_file_byte_at $1 $((addr + 8))
+}
+
+function replace_vid_msb () {
+    replace_file_byte_at $1 $((addr + 9))
+}
+
+function replace_pid_lsb () {
+    replace_file_byte_at $1 $((addr + 10))
+}
+
+function replace_pid_msb () {
+    replace_file_byte_at $1 $((addr + 11))
+}
+
+function replace_bcd_device_lsb () {
+    replace_file_byte_at $1 $((addr + 12))
+}
+
+function replace_bcd_device_msb () {
+    replace_file_byte_at $1 $((addr + 13))
+}
index dd6ed28..48e29c4 100755 (executable)
@@ -6,7 +6,7 @@ nl=$'\n'
 #
 # This file is *NOT* generated by GNU Autoconf, but written by NIIBE Yutaka
 #
-# Copyright (C) 2010, 2011, 2012, 2013, 2015, 2016, 2017
+# Copyright (C) 2010, 2011, 2012, 2013, 2015, 2016, 2017, 2018
 #               Free Software Initiative of Japan
 #
 # This file is a part of NeuG, a Random Number Generator implementation.
@@ -279,8 +279,32 @@ else
 fi
 
 output_vid_pid_version () {
-  echo "$VIDPID" | sed -n -e "s%^\([0-9a-f][0-9a-f]\)\([0-9a-f][0-9a-f]\):\([0-9a-f][0-9a-f]\)\([0-9a-f][0-9a-f]\)$%  0x\2, 0x\1, /* idVendor  */\\${nl}  0x\4, 0x\3, /* idProduct */%p"
-  echo "$VERSION" | sed -n -e "s%^\([0-9a-f][0-9a-f]\)\([0-9a-f][0-9a-f]\)$%  0x\2, 0x\1, /* bcdDevice */%p"
+  echo "$VIDPID" | \
+  sed -n -e "s%^\([0-9a-f][0-9a-f]\)\([0-9a-f][0-9a-f]\):\([0-9a-f][0-9a-f]\)\([0-9a-f][0-9a-f]\)$%\1\t\2\t\3\t\4%p" | \
+  while read -r FIRST SECOND THIRD FOURTH; do
+    if test $FIRST != 00; then
+      echo replace_vid_msb $FIRST
+    fi
+    if test $SECOND != 00; then
+      echo replace_vid_lsb $SECOND
+    fi
+    if test $THIRD != 00; then
+      echo replace_pid_msb $THIRD
+    fi
+    if test $FOURTH != 00; then
+      echo replace_pid_lsb $FOURTH
+    fi
+  done
+  echo "$VERSION" | \
+  sed -n -e "s%^\([0-9a-f][0-9a-f]\)\([0-9a-f][0-9a-f]\)$%\1\t\2%p" | \
+  while read -r FIRST SECOND; do
+    if test $FIRST != 00; then
+      echo replace_bcd_device_msb $FIRST
+    fi
+    if test $SECOND != 00; then
+      echo replace_bcd_device_lsb $SECOND
+    fi
+  done
 }
 
 output_vendor_product_serial_strings () {
@@ -332,14 +356,22 @@ output_vendor_product_serial_strings () {
   fi
 }
 
+(echo "#! /bin/bash"
+ echo
+ echo '. "binary-edit.sh"') > put-vid-pid-ver.sh
+
 if !(IFS="     "
   while read -r VIDPID VERSION PRODUCT VENDOR; do
     if test "$vidpid" = "$VIDPID"; then
-      output_vid_pid_version > usb-vid-pid-ver.c.inc
+      echo                       >> put-vid-pid-ver.sh
+      echo 'addr=$file_off_ADDR' >> put-vid-pid-ver.sh
+      output_vid_pid_version     >> put-vid-pid-ver.sh
       output_vendor_product_serial_strings neug_ >usb-strings.c.inc
       exit 0
     elif test "$with_fraucheky" = "$VIDPID"; then
-      output_vid_pid_version > fraucheky-vid-pid-ver.c.inc
+      echo                                 >> put-vid-pid-ver.sh
+      echo 'addr=$file_off_fraucheky_ADDR' >> put-vid-pid-ver.sh
+      output_vid_pid_version               >> put-vid-pid-ver.sh
       output_vendor_product_serial_strings >fraucheky-usb-strings.c.inc
     fi
   done; exit 1) < ../NEUG_USB_DEVICE_ID
index e216eba..2bd169f 100644 (file)
@@ -100,7 +100,9 @@ uint8_t vcom_device_desc[18] = {
   0x00,                                /* bDeviceSubClass.                 */
   0x00,                                /* bDeviceProtocol.                 */
   0x40,                                /* bMaxPacketSize.                  */
-#include "usb-vid-pid-ver.c.inc"
+  0x00, 0x00,                  /* idVendor  (will be replaced)     */
+  0x00, 0x00,                  /* idProduct (will be replaced)     */
+  0x00, 0x00,                  /* bcdDevice (will be replaced)     */
   1,                           /* iManufacturer.                   */
   2,                           /* iProduct.                        */
   3,                           /* iSerialNumber.                   */