CGO inside Windows

What is CGO?

CGO lets Go packages call C code

Installing MSYS2

  • Download installer from https://www.msys2.org/ (current it’s msys2-x86_64-20200903.exe)
  • After install, inside MSYS terminal, using pacman, install gcc
pacman -S \
 mingw-w64-x86_64-binutils \
 mingw-w64-x86_64-crt-git \
 mingw-w64-x86_64-gcc \
 mingw-w64-x86_64-gcc-libs \
 mingw-w64-x86_64-gdb  \
 mingw-w64-x86_64-headers-git \
 mingw-w64-x86_64-libmangle-git   \
 mingw-w64-x86_64-libwinpthread-git \
 mingw-w64-x86_64-make \
 mingw-w64-x86_64-pkg-config \
 mingw-w64-x86_64-tools-git \
 mingw-w64-x86_64-winpthreads-git \
 mingw-w64-x86_64-winstorecompat-git 
  • now, configure bash initialization
# inside: ~/.bashrc

alias ll="ls -la --color"

export PATH=/s/go/bin:$PATH
export GOPATH=/w/work/go

Configure Windows Terminal

As explained in official guide…

{
 "guid": "{17da3cac-b318-431e-8a3e-7fcdefe6d114}",
 "name": "MINGW64 / MSYS2",
 "commandline": "S:/msys64/msys2_shell.cmd -defterm -here -no-start -mingw64",
 //"startingDirectory": "C:/msys64/home/%USERNAME%",
 "startingDirectory": "W:/work",
 "icon": "S:/msys64/mingw64.ico",
 "fontSize": 12,
 "colorScheme": "GitBash"
},
{
 "guid": "{2d51fdc4-a03b-4efe-81bc-722b7f6f3820}",
 "name": "MINGW32 / MSYS2",
 "commandline": "S:/msys64/msys2_shell.cmd -defterm -here -no-start -mingw32",
 // "startingDirectory": "C:/msys64/home/%USERNAME%",
 "startingDirectory": "W:/work",
 "icon": "S:/msys64/mingw32.ico",
 "fontFace": "Lucida Console",
 "fontSize": 12
},
{
 "guid": "{71160544-14d8-4194-af25-d05feeac7233}",
 "name": "MSYS / MSYS2",
 "commandline": "S:/msys64/msys2_shell.cmd -defterm -here -no-start -msys",
 // "startingDirectory": "C:/msys64/home/%USERNAME%",
 "startingDirectory": "W:/work",
 "icon": "S:/msys64/msys2.ico",
 "fontFace": "Lucida Console",
 "fontSize": 14
}

Testing

cd $GOPATH
go get github.com/mattn/go-sqlite3
go install github.com/mattn/go-sqlite3

Windows Terminal Git Bash

How to add Git Bash into Windows Terminal

Inside Windows Terminal, press CTRL+, to start editing “settings.json”

{
   "profiles": {
      "list": [
         {
            "guid": "{f9c3b687-50b7-4b13-b312-c28e955ed7d3}",
            "hidden": false,
            "name": "Git Bash",
            "commandline": "S:/app/Git/usr/bin/bash.exe -i -l",
            "startingDirectory": "W:/work",
            "icon": "S:/app/Git/mingw64/share/git/git-for-windows.ico",
            "snapOnInput": true,
            "closeOnExit": true,
            "colorScheme": "GitBash",
            "fontFace": "Consolas",
            "fontSize": 14
         }
      ]
   }
}

After this, add a custom color scheme named “GitBash”

"schemes": [
   {
      "name": "GitBash",
      "background": "#FFFFF0",
      "foreground": "#404040",
      "black": "#0C0C0C",
      "blue": "#6060ff",
      "brightBlack": "#767676",
      "brightBlue": "#3B78FF",
      "brightCyan": "#61D6D6",
      "brightGreen": "#16C60C",
      "brightPurple": "#B4009E",
      "brightRed": "#E74856",
      "brightWhite": "#F2F2F2",
      "brightYellow": "#F9F1A5",
      "cyan": "#3A96DD",
      "green": "#00a400",
      "purple": "#bf00bf",
      "red": "#bf0000",
      "white": "#ffffff",
      "yellow": "#bfbf00",
      "grey": "#bfbfbf"
   }
],

REFERENCES

https://stackoverflow.com/questions/56839307/adding-git-bash-to-the-new-windows-terminal

Windows 10 permissions

RunAs Administrator:

REM Backup permissions of WindowsApps
icacls "%ProgramFiles%\WindowsApps" /save WindowsApps.acl

REM change owner to copy directory (it's a LOT of files!!!)
takeown /F “C:\Program Files\WindowsApps” /A /R

robocopy "c:\Program Files" s:\C_ProgramFiles /COPYALL /MIR /LOG:robocopy.txt /ZB /DCOPY:T /V /TEE

REM restore permissions
icacls "%ProgramFiles%" /restore WindowsApps.acl
RunAs Administrator:

cd some_directory
icacls * /t /q /c /reset
takeown /R /F *
icacls * /t /grant Everyone:F
cd ..
REM List all symbolic links and directory junctions
DIR /AL /S C:\

Junctions vs Directory symbolic link

https://superuser.com/questions/343074/directory-junction-vs-directory-symbolic-link

icacls reference

Windows 10 reset NTFS permissions

RunAs Administrator:

cd some_directory
icacls * /t /q /c /reset
takeown /R /F *
icacls * /t /grant Everyone:F
cd ..
REM to save and restore permissions
icacls _directory_ /save _filename_
icacls _directory_ /restore _filename_


REM Backup permissions of WindowsApps
icacls "%ProgramFiles%\WindowsApps" /save WindowsApps.acl /t

REM restore permissions
icacls "%ProgramFiles%" /restore WindowsApps.acl

Reference: icacls reference

MSX RS232C BIOS Call spec

Mirror from: http://www.msxarchive.nl/pub/msx/mirrors/msx2.com/sources/232bcal.txt

   RS232C Extended BIOS Call specification

                                September 18th, 1984
                                December  24th, 1985








































RS232C Extended BIOS Call specification                         Page 2


1.0  GENERAL DESCRIPTION

        The  RS232C  driver  can be  used by  application programs  using the
        "EXTENDED BIOS CALL" mechanism. Such user can access each function in
        the  RS232C driver  through the entry jump table with inter-slot call
        funtion provided  in the  BIOS. The user is able to know the location
        of  this table  by EXTENDED  BIOS CALL 0 and 1. Refer to the document
        "Specification of the EXTENDED BIOS CALL" for details.




1.1  EXTENDED BIOS CALL ENTRY TABLE

        The RS232C  driver has  entries as  follows. A application program is
        able to use RS232C driver by 'inter-slot call' to those entries.

For I/O port only type of RS-232C interface.

EXBTBL: DEFB    DVINFB          ; device information
        DEFB    0               ; reserved for future expansion
        DEFB    0
        JP      INIT            ; initialize RS232C port
        JP      OPEN            ; Open RS232C port
        JP      STAT            ; ReaD STATus
        JP      GETCHR          ; receive data
        JP      SNDCHR          ; send data
        JP      CLOSE           ; close RS232C port
        JP      EOF             ; tell EOF code received
        JP      LOC             ; reports number of characters in the
                                ; receiver buffer
        JP      LOF             ; reports number of free space left in
                                ; receiver buffer
        JP      BACKUP          ; back up a character
        JP      SNDBRK          ; send a break character
        JP      DTR             ; turn on/off DTR line
        NOENT
        NOENT
        NOENT
























RS232C Extended BIOS Call specification                         Page 3



Multi channel type RS-232C cartridge

EXBTBL:
        DEFB    DVINFB
        DEFB    1               ; version number
        DEFB    0               ; reserved for future expansion
        JP      INIT            ; initialize RS232C port
        JP      OPEN            ; open RS232C port
        JP      STAT            ; ReaD STATus
        JP      GETCHR          ; reveive data
        JP      SNDCHR          ; send data
        JP      CLOSE           ; close RS232C port
        JP      EOF             ; tell EOF code received
        JP      LOC             ; reports number of characters in the
                                ; receiver buffer
        JP      LOF             ; reports number of free space left in the
                                ; receiver buffer
        JP      BACKUP          ; back up a character
        JP      SNDBRK          ; send break character
        JP      DTR             ; turn on/off DTR line
        JP      SETCHN          ; set channel number
        NOENT
        NOENT



                                NOTE

                The  RS232C  receiver  is  driven  by the
                interrupt  generated  by  receiver ready.
                However,   the  inter-slot  call  handler
                disables  interrupt   automatically.  So,
                when  control returns to  the application
                program,  it has  to enable  interrupt as
                soon  as   possible.   Otherwise,  RS232C
                receiver    routine    loses     incoming
                characters.

























RS232C Extended BIOS Call specification                         Page 4



2.0  DESCRIPTION OF EACH EXTENDED BIOS CALL

2.1  Initialize RS232C Port (INIT)


        Entry:  [HL]= address of the parameter table
                [B] = slot address of the parameter table
        Return: carry flag is set if illegal parameters are contained
        Modify: [AF]

        Description:
         To initialize  the RS232C  port with specified parameter. This entry
         must  be  called  before  any  other  function  calls are  made. The
         parameters  are  similar  to  _COMINI  expanded statement  of BASIC.
         However,  note that  all the ascii parameters must be specified with
         upper case characters only.

        BAUD RATE
         It  is  possible  to  set different  baud rate  for transmitter  and
         receiver. The possible value for the baud rate are as follows:

         50, 75, 110, 300, 600, 1200, 1800, 2000, 2400, 3600, 4800,
         7200, 9600, 19200

         When negative  value is  specified, its absolute value is written to
         i8253 timer/counter directly.


        TIME OUT
         The  RS232C  driver waits  till the  CTS (Clear  To Send)  signal is
         turned  on and/or  XON is received when the character is to be sent.
         The driver  will generates time out error when it waits for them, if
         specified  time passed.  Its time is specified this value at second.
         If 0  specified then  the driver doesn't generate time out error and
         waits forever.



























RS232C Extended BIOS Call specification                         Page 5



                +-------------------------------+
    [B]:[HL]--> | Character length '5'-'8'      |\
                +-------------------------------+ \
                | Parity 'E','O','I','N'        |  |
                +-------------------------------+  |
                | Stop bits '1','2','3'         |  |
                +-------------------------------+   >-- ASCII
                | XON/XOFF controll 'X','N'     |  |   character
                +-------------------------------+  |
                | CTR-RTS hand shake 'H','N'    |  |
                +-------------------------------+  |
                | Auto LF for receive 'A','N'   |  |
                +-------------------------------+  |
                | Auto LF for send 'A','N'      |  |
                +-------------------------------+ /
                | SI/SO control 'S','N'         |/
                +-------------------------------+
                | Receiver baud rate    (low)   |\
                +---                        ----+ \
                | 50-19200              (high)  |  |
                +-------------------------------+  |
                | Transmitter baud rate (low)   |   >-- Binary
                +---                        ----+  |
                | 50-19200              (high)  |  |
                +-------------------------------+ /
                | Time out counter 0-255        |/
                +-------------------------------+



































RS232C Extended BIOS Call specification                         Page 6



2.2  Open RS232C Port (OPEN)

        Entry:  [HL]= address of FCB (must be located higher address
                      than 8000H)
                [C] = buffer length ( 32-254 )
                [E] = open mode, one of following:
                 +----------+------------------------------+
                 |open mode |   meanings                   |
                 +----------+------------------------------+
                 |    1     |  mode                 |
                 |    2     |  mode                |
                 |    4     |  and  mode|
                 +----------+------------------------------+
        Return: carry flag is set if any error occured.
        Modify: [AF]

        Description:
         Opens  RS232C port  with specified  FCB (File  Control Block). Opens
         must be  made before  any I/O  operations take  place. Each received
         character  occupies  two  bytes  in  the  buffer.  One  is  received
         character  code itself  and another  is error status of the received
         character. And  extra 9 bytes are necessary as a working storage for
         the  file  control.  Note  that  the  buffer  length  passed  by [C]
         specifies  number of  characters, so  the actual length of buffer is
         [C] x 2 + 9 bytes. And this buffer area can be accessed without slot
         handling whenever  the RS232C driver is called (including the timing
         when the interrupt from the the receiver generated).

                |                               |
                +-------------------------------+
                | 9 bytes for file control      |
                |                               |
                +-------------------------------+
                | [C] x 2 bytes receiver buffer |
                |                               |
                +-------------------------------+
                |                               |

























RS232C Extended BIOS Call specification                         Page 7



2.3  Read Status (STAT)

        Entry:  None
        Return: [HL]= status data.
        Modify: None

        Description:
         Returns  various status  information and error code of the character
         just read from the buffer (not the character just received).
    +---------+-------------------------------------------------+
    | BIT NO. |       Description                               |
    +---------+-------------------------------------------------+
    |   15    | Buffer over flow error                          |
    |         |   0 - no buffer over flow                       |
    |         |   1 - buffer over flow                          |
    |   14    | Time out error                                  |
    |         |   0 - no time out error occured                 |
    |         |   1 - time out error occured                    |
    |   13    | Framing error                                   |
    |         |   0 - no framing error occured                  |
    |         |   1 - framing error occured                     |
    |   12    | Over run error                                  |
    |         |   0 - no over run error occured                 |
    |         |   1 - over run error occured                    |
    |   11    | Parity error                                    |
    |         |   0 - the character hasn't parity error         |
    |         |   1 - the character has parity error            |
    |   10    | Control break key was pressed                   |
    |         |   0 - control break key wasn't pressed          |
    |         |   1 - control break key was pressed             |
    |    9    | Not used, reserved                              |
    |    8    | Not used, reserved                              |
    |    7    | Clear To Send                                   |
    |         |   0 - false                                     |
    |         |   1 - true                                      |
    |    6    | Timer/counter output-2                          |
    |         |   0 - timer/counter output-2 is negated         |
    |         |   1 - timer/counter output-2 is asserted        |
    |    5    | Not used, reserved                              |
    |    4    | Not used, reserved                              |
    |    3    | Data Set Ready                                  |
    |         |   0 - false                                     |
    |         |   1 - true                                      |
    |    2    | break detect                                    |
    |         |   0 - not yet detect                            |
    |         |   1 - detect                                    |
    |    1    | Ring Indicator                                  |
    |         |   0 - false                                     |
    |         |   1 - true                                      |
    |    0    | Carrier Detect                                  |
    |         |   0 - false                                     |
    |         |   1 - true                                      |
    +---------+-------------------------------------------------+









RS232C Extended BIOS Call specification                         Page 8



2.4  Get A Character From The Receive Buffer (GETCHR)

        Entry:  None
        Return: [A] = character received
                sign flag is set if any error occured.
                carry flag  is set  if the character is an EOF code when port
                is opened for input mode.
        Modify: [F]

        Description:
         Get  a  character  from  the  receiver  buffer.  Returns  backed  up
         character if any.




2.5  Send A Character To The RS232C Port (SNDCHR)

        Entry:  [A] = character to send
        Return: carry flag is set if control break key was pressed
                zero flag is set if time out error occured during waiting for
                XON or/and CTS signal.
        Modify: [F]

        Description:
         Send  specified character to RS232C port. The character flow control
         by XON/XOFF  characters and/or  CTS (Clear  To Send)  line signal is
         handled  if initialized  so. Time  out error  will be generated when
         specified time passed during waiting for permision for transmission,
         and the character will not be sent.




2.6  Close The RS232C Port (CLOSE)

        Entry:  None
        Return: carry is set if any error occured
        Modify: [AF]

        Description:
         Closes the  RS232C port.  The buffer  is released, and a EOF code is
         sent  if the port was opened for  mode. RTS signal is turned
         to inactive state.


















RS232C Extended BIOS Call specification                         Page 9



2.7  Check For The EOF Code (EOF)

        Entry:  None
        Return: [HL]= -1, carry flag is set, if the next character is EOF
                      code.
                    =  0, carry flag is reset, if the next character is not
                      EOF code.
        Modify: [AF]

        Description:
         Tests whether the next character is EOF code or not. Returns 0 if no
         character.



2.8  Returns A Number Of Character In The Receive Buffer (LOC)

        Entry:  None
        Return: [HL] = number of character in the receiver buffer
        Modify: [AF]

        Description:
         Returns  number of valid character in the receive buffer. This value
         includes number  of backed  up character.  Characters after EOF code
         are  ignored if  opened in   mode,  but occupies buffer space
         although.



2.9  Returns Number Of Free Space In The Receive Buffer (LOF)

        Entry:  None
        Return: [HL] = number of free space
        Modify: [AF]

        Description:
         Returns a number of free space in character in the receiver buffer.



2.10  Back Up A Character (BACKUP)

        Entry:  [C] = character to back up
        Return: None
        Modify: [F]

        Description:
         Backs up a character in the special buffer. Last backed up character
         will be lost if any.













RS232C Extended BIOS Call specification                         Page 10




2.11  Send Break Character (SNDBRK)

        Entry:  [DE] = number of break character to send
        Return: carry flag is set if control break key was pressed
        Modify: [AF], [DE]

        Description:
         Transmit   specified   number   of   break  characters.   Aborts  if
         Control-Break key  is pressed  during the  transmission and  returns
         with carry flag set.



2.12  Turn On/off DTR Line (DTR)

        Entry:  [A] = 0 if turn off
                [A] = not 0 if turn on
        Return: None
        Modify: [F]

        Description:
         DTR  (Data Terminal  Ready) line  is turned  on when  power-on/reset
         initializing or INIT routine is called.



3.0  MULTIPLE CHANNEL TYPE RS-232C CARTRIDGE

3.1  Set Channel Number (SETCHN)

        Entry:  [A] = channel number.
        Return: Carry  flag is  set if  the channel  is not in the cartridge.
                Otherwise the channel is in the cartridge.
        Modify: [AF], [BC]

        Description:
         The channel number is set with 0 when power-on/reset initializing.
























RS232C Extended BIOS Call specification                         Page 11







                                APPENDIX A

                        MISCELLANEOUS INFORMATIONS




A.1  THE BEHAVIOR OF CONTROL SIGNALS

                RESET        COMINI       OPEN        CLOSE
        ---------------------------------------------------------
         RTS - inactive    no effect     active      inactive
         DTR - active        active     no effect   no effect

RTS signal is affected in following cases:

  1. OPEN statement is executed - activated.
  2. CLOSE statement is executed - inactivated.
  3. The  rest  of the  communication buffer is less than 16 byte and CTS-RTS
     handshake is enabled - inactivated.
  4. When  it is  inactive and  the rest  of the communication buffer becomes
     more than byte and CTS-RTS handshake is enabled - activated.

DTR signal is affected by CALL COMDTR and CALL COMINI statements.



A.2  HANDLING OF EOF

EOF is transmitted when close is executed when the open mode was output.

Xubuntu sound popping every few seconds SOLVED

The popping is caused by power saving feature that can cause audible pops when your sound card sleeps and wakes up. So in my desktop I disabled this feature!

My sound card is an HDA-Intel

cat /proc/asound/cards

0 [PCH ]: HDA-Intel - HDA Intel PCH
          HDA Intel PCH at 0xf7f30000 irq 34

To disable power saving and persist this change after reboot:

>>> /etc/modprobe.d/alsa-base.conf <<< add
options snd-hda-intel power_save=0 power_save_controller=N

To test settings (in my case doesn’t work)

sudo sh -c "echo 0 >  /sys/module/snd_hda_intel/parameters/power_save"
sudo sh -c "echo N > /sys/module/snd_hda_intel/parameters/power_save_controller"

REF: https://superuser.com/questions/1493096/linux-ubuntu-speakers-popping-every-few-seconds

Xubuntu 20.04 with LVM on LUKS

Boot Xubuntu installation media, choose “Try”, enter “Terminal emulator” …

First, create three partitions: EFI with 128MB, /boot with 512MB and last with remaining space

sudo fdisk /dev/sda
g

n
1
<ENTER>
+128M
<remove signature>
t
1

n
2
<ENTER>
+512M
t
2
20

n
3
<ENTER>
<ENTER>
t
3
31
Disk model: ST1000VN002-2E41
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: A2C8ED15-47CB-4E4B-929E-E45128441F03
Device Start End Sectors Size Type
/dev/sda1 2048 264191 262144 128M BIOS boot
/dev/sda2 264192 1312767 1048576 512M Linux filesystem
/dev/sda3 1312768 1953525134 1952212367 930.9G Linux LVM
sudo mkfs.fat -F32 /dev/sda1
sudo mkfs.ext2 -m 0 /dev/sda2
sudo cryptsetup luksFormat --hash=sha512 --key-size=512 --cipher=aes-xts-plain64 --verify-passphrase /dev/sda3
sudo cryptsetup luksOpen /dev/sda3 sda3_crypt

sudo pvcreate /dev/mapper/sda3_crypt

sudo vgcreate vg0 /dev/mapper/sda3_crypt

sudo lvcreate -n dskswap -L 16GB vg0
sudo lvcreate -n dskroot -L 100GB vg0
sudo lvcreate -n dskhome -L 100GB vg0
sudo mkswap /dev/mapper/vg0-dskswap
sudo swaplabel -L SWAP_SDA /dev/mapper/vg0-dskswap
sudo mkfs.ext4 /dev/mapper/vg0-dskroot
sudo mkfs.ext4 /dev/mapper/vg0-dskhome

Now, execute “Install Xubuntu 20.04 LTS” with icon on Desktop. Next, next, next… until “Instalation Type”, choose “Something else”

Devicetypemount point
/dev/mapper/vg0-dskhomeext4/home
/dev/mapper/vg0-dskhomeext4/
/dev/mapper/vg0-dskswapswapswap
/dev/sda1fat32/boot/efi
/dev/sda2ext2/boot
Xubuntu 20.04 custom install disk partitions

Device for boot loader installation: /dev/sda

Choose “continue testing”… Now build the chroot environment

Chroot environment

sudo cryptsetup luksOpen /dev/sda3 sda3_crypt
mkdir newpart
sudo mount /dev/mapper/vg0-dskroot newpart
sudo mount /dev/sda2 newpart/boot
sudo mount /dev/sda1 newpart/boot/efi

sudo mount -t proc /proc newpart/proc
sudo mount --rbind /dev newpart/dev
sudo mount --rbind /dev/pts newpart/dev/pts
sudo mount --rbind /sys newpart/sys 
sudo mount --rbind /run newpart/run
sudo chroot newpart

Now, prepare LUKS automount

sudo cryptsetup luksDump /dev/sda3 | grep UUID

>>> /etc/crypttab <<<
sda3_crypt UUID=2945b973-12ea-4f45-9454-8d873da8be9d none luks,discard

Test automount

$ sudo /sbin/cryptdisks_start sda3_crypt
/dev/mapper/vg0-dskroot / ext4 errors=remount-ro 0 1
UUID=45bf98f6-665e-4d9d-9462-e40b017e3881 /boot ext2 defaults 0 2
UUID=662E-6E71 /boot/efi vfat utf8,umask=007,gid=46 0 1
/dev/mapper/vg0-dskhome /home ext4 defaults 0 2
/dev/mapper/vg0-dskswap none swap sw 0 0
apt-get install grub-efi-amd64 efibootmgr

grub-install --target=x86_64-efi /dev/sda

efibootmgr --create --disk /dev/sda --part 1 --loader /boot/efi/EFI/BOOT/BOOTX64.EFI --label "Xubuntu Boot Manager"


## efibootmgr --create --disk /dev/sda --part 1 --loader /boot/efi/EFI/ubuntu/grubx64.efi --label "Xubuntu Boot Manger" --verbose


exit
reboot






# update-grub
# grub-install --recheck /dev/sda
# update-initramfs -u

reboot