diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..bacf933 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,113 @@ +name: Make CI + +on: + push: + branches: [ main ] + release: + types: + - published + pull_request: + workflow_dispatch: + +jobs: + build-linux: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + + - name: Install libudev and libhidapi + run: sudo apt-get install -y libudev-dev libhidapi-dev + + - name: Compile SonixFlasherC + run: | + make clean package + + - name: Find artifact files + id: find-files + run: | + find . -type f -name "sonixflasher-*.zip" -print > artifact_files.txt + files=$(cat artifact_files.txt) + echo "FILES=$files" >> $GITHUB_ENV + + - uses: actions/upload-artifact@v3.0.0 + with: + name: SonixFlasherC-Linux + path: ${{ env.FILES }} + + build-mac: + runs-on: macos-latest + + steps: + - uses: actions/checkout@v3 + + - name: Install hidapi + run: | + brew update + brew install hidapi + + - name: Compile SonixFlasherC + run: | + make clean package + + - name: Find artifact files + id: find-files + run: | + find . -type f -name "sonixflasher-*.zip" -print > artifact_files.txt + files=$(cat artifact_files.txt) + echo "FILES=$files" >> $GITHUB_ENV + + - uses: actions/upload-artifact@v3.0.0 + with: + name: SonixFlasherC-macOS + path: ${{ env.FILES }} + + build-windows: + runs-on: windows-latest + defaults: + run: + shell: msys2 {0} + + steps: + - uses: actions/checkout@v3 + - uses: msys2/setup-msys2@v2 + with: + msystem: MINGW64 + update: true + install: git p7zip dos2unix mingw-w64-x86_64-pkg-config mingw-w64-x86_64-cc mingw-w64-x86_64-hidapi make + + - name: Compile SonixFlasherC + run: | + make clean sonixflasher package + + - name: Find artifact files + id: find-files + run: | + find . -type f -name "sonixflasher-*.zip" -print > artifact_files.txt + files=$(cat artifact_files.txt) + echo "FILES=$files" >> $GITHUB_ENV + + - uses: actions/upload-artifact@v3.0.0 + with: + name: SonixFlasherC-Windows + path: ${{ env.FILES }} + + publish_release: + name: Publish (Release) + runs-on: ubuntu-latest + + needs: [build-linux, build-mac, build-windows] + + if: github.event.release.tag_name + + steps: + - uses: actions/download-artifact@v3 + + - uses: softprops/action-gh-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + name: ${{ github.event.release.name }} + files: | + ${{ github.workspace }}/*/* + fail_on_unmatched_files: true diff --git a/.github/workflows/c-cpp.yml b/.github/workflows/c-cpp.yml deleted file mode 100644 index ec4ca7a..0000000 --- a/.github/workflows/c-cpp.yml +++ /dev/null @@ -1,80 +0,0 @@ -name: Make CI - -on: - push: - branches: [ main ] - workflow_dispatch: - -jobs: - build-linux: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v2 - with: - submodules: recursive - - - name: Install libudev - run: sudo apt-get install -y libudev-dev - - - name: Compile SonixFlasherC - run: | - make sonixflasher - tar -cvf sonixflasherc-linux.tar.xz sonixflasher - - - uses: actions/upload-artifact@v3.0.0 - with: - name: SonixFlasherC Linux - path: sonixflasherc-linux.tar.xz - - build-mac: - runs-on: macos-latest - - steps: - - uses: actions/checkout@v2 - with: - submodules: recursive - - - name: Setup Arch - run: export ARCHFLAGS="-arch x86_64" - - - name: Compile SonixFlasherC - run: | - make sonixflasher - zip sonixflasherc-mac.zip sonixflasher - - - uses: actions/upload-artifact@v3.0.0 - with: - name: SonixFlasherC Mac - path: sonixflasherc-mac.zip - - build-windows: - runs-on: windows-latest - defaults: - run: - shell: msys2 {0} - - steps: - - uses: actions/checkout@v2 - with: - submodules: recursive - - - uses: msys2/setup-msys2@v2 - with: - msystem: MINGW64 - update: true - install: git mingw-w64-x86_64-cc make - - - name: Compile SonixFlasherC - run: | - make sonixflasher - - - name: Zip executable - run: Compress-Archive -Path sonixflasher.exe -DestinationPath sonixflasherc-win.zip - shell: powershell - - - uses: actions/upload-artifact@v3.0.0 - with: - name: SonixFlasherC Windows - path: sonixflasherc-win.zip - diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index d8b6bbe..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "hidapi"] - path = hidapi - url = https://github.com/libusb/hidapi diff --git a/Makefile b/Makefile index 9a5a827..c11c91b 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,3 @@ -# path for HID -HIDAPI_DIR ?= ./hidapi - # try to do some autodetecting UNAME := $(shell uname -s) ARCH := $(shell uname -m) @@ -24,9 +21,8 @@ endif ############# Mac ifeq "$(OS)" "macos" -CFLAGS+=-arch x86_64 -arch arm64 -LIBS=-framework IOKit -framework CoreFoundation -framework AppKit -OBJS=$(HIDAPI_DIR)/mac/hid.o +CFLAGS+=`pkg-config hidapi --cflags` +LIBS=-lhidapi -framework IOKit -framework CoreFoundation -framework AppKit EXE= endif @@ -34,8 +30,8 @@ endif ############# Windows ifeq "$(OS)" "windows" -LIBS += -lsetupapi -Wl,--enable-auto-import -static-libgcc -static-libstdc++ -OBJS = $(HIDAPI_DIR)/windows/hid.o +CFLAGS+=`pkg-config hidapi --cflags` +LIBS+= -lhidapi -lsetupapi -Wl,--enable-auto-import -static-libgcc -static-libstdc++ EXE=.exe endif @@ -44,7 +40,8 @@ endif ifeq "$(OS)" "linux" LIBS = `pkg-config libudev --libs` -OBJS = $(HIDAPI_DIR)/linux/hid.o +CFLAGS+=`pkg-config hidapi-libusb --cflags` +LIBS+=`pkg-config hidapi-libusb --libs` EXE= endif @@ -52,7 +49,7 @@ endif ############# common -CFLAGS+=-Wall -I $(HIDAPI_DIR)/hidapi +CFLAGS+=-Wall OBJS += sonixflasher.o all: sonixflasher @@ -70,4 +67,4 @@ clean: package: sonixflasher$(EXE) @echo "Packaging up sonixflasher for '$(OS)-$(ARCH)'" - zip sonixflasher-$(OS)-$(ARCH).zip sonixflasher$(EXE) + 7z a sonixflasher-$(OS)-$(ARCH).zip sonixflasher$(EXE) diff --git a/README.md b/README.md index 4e6dfd2..c178d94 100644 --- a/README.md +++ b/README.md @@ -16,11 +16,12 @@ Features: ### Dependencies -Clone this repository recursively: +Clone this repository: ``` -git clone --recursive https://github.com/SonixQMK/SonixFlasherC +git clone https://github.com/SonixQMK/SonixFlasherC ``` +[hidapi](https://github.com/libusb/hidapi) is a prequisite. If using windows, please install MinGW ### Compiling diff --git a/hidapi b/hidapi deleted file mode 160000 index e08ddce..0000000 --- a/hidapi +++ /dev/null @@ -1 +0,0 @@ -Subproject commit e08ddcee5f597e6bf77e95ffc909feab05fdfd4c diff --git a/sonixflasher.c b/sonixflasher.c index cf561de..1027e0d 100644 --- a/sonixflasher.c +++ b/sonixflasher.c @@ -5,6 +5,7 @@ #include #include #include +#include #include @@ -27,6 +28,9 @@ #define MAX_ATTEMPTS 5 +#define PROJECT_NAME "sonixflasher" +#define PROJECT_VER "1.0.0" + long MAX_FIRMWARE = MAX_FIRMWARE_SN32F260; static void print_usage(char *m_name) @@ -39,6 +43,7 @@ static void print_usage(char *m_name) " --offset -o Set flashing offset (default: 0)\n" " --file -f Binary of the firmware to flash (*.bin extension) \n" " --jumploader -j Define if we are flashing a jumploader \n" + " --version -V Print version information\n" "\n" "Examples: \n" ". Flash jumploader to device w/ vid/pid 0x0c45/0x7040 \n" @@ -50,6 +55,12 @@ static void print_usage(char *m_name) "", m_name); } +//Display program version +static void display_version(char *m_name) +{ + fprintf(stderr,"%s " PROJECT_VER "\n",m_name); +} + void clear_buffer(unsigned char *data, size_t lenght) { for(int i = 0; i < lenght; i++) data[i] = 0; @@ -145,7 +156,7 @@ bool flash(hid_device *dev, long offset, FILE *firmware, long fw_size, bool skip clear_buffer(buf, 65); write_buffer_32(buf, CMD_PREPARE); write_buffer_32(buf+5, (uint32_t)offset); - write_buffer_32(buf+9, (uint32_t)(fw_size/64)); + write_buffer_32(buf+9, (uint32_t)(fw_size/64)); hid_set_feature(dev, buf, 65); clear_buffer(buf, 65); @@ -241,18 +252,20 @@ int main(int argc, char* argv[]) uint16_t pid = 0; long offset = 0; char* file_name = NULL; + char* endptr = NULL; bool flash_jumploader = false; if(argc < 2) { - print_usage("sonixflasher"); + print_usage(PROJECT_NAME); exit(1); } struct option longoptions[] = { {"help", no_argument, 0, 'h'}, + {"version", no_argument, 0, 'V'}, {"vidpid", required_argument, NULL, 'v'}, {"offset", optional_argument, NULL, 'o'}, {"file", required_argument, NULL, 'f'}, @@ -260,12 +273,15 @@ int main(int argc, char* argv[]) {NULL,0,0,0} }; - while ((opt = getopt_long(argc, argv, "hv:o:f:j", longoptions, &opt_index)) != -1) + while ((opt = getopt_long(argc, argv, "hVv:o:f:j", longoptions, &opt_index)) != -1) { switch (opt) { - case 0: // Show help - print_usage("sonixflasher"); + case 'h': // Show help + print_usage(PROJECT_NAME); + break; + case 'V': // version + display_version(PROJECT_NAME); break; case 'v': // Set vid/pid if( sscanf(optarg, "%4hx/%4hx", &vid,&pid) !=2 ) { // match "23FE/AB12" @@ -276,12 +292,21 @@ int main(int argc, char* argv[]) vid = wordbuf[0]; pid = wordbuf[1]; } } + // make sure we have the correct vidpid + if(vid == 0 || pid == 0) { + fprintf(stderr, "ERROR: invalid vidpid -'%s'.\n",optarg); + exit(1); + } break; case 'f': // file name file_name = optarg; break; case 'o': // offset - offset = strtol(optarg,NULL, 0); + offset = strtol(optarg, &endptr, 0); + if (errno == ERANGE || *endptr != '\0') { + fprintf(stderr, "ERROR: invalid offset value -'%s'.\n", optarg); + exit(1); + } break; case 'j': // Jumploader flash_jumploader = true; @@ -292,6 +317,7 @@ int main(int argc, char* argv[]) { case 'f': case 'v': + case 'o': fprintf(stderr, "ERROR: option '-%c' requires a parameter.\n", optopt); break; case 0: @@ -303,7 +329,8 @@ int main(int argc, char* argv[]) } exit(1); } - + // exit clean after printing + if(opt == 'h' || opt == 'V') exit(1); } if (file_name == NULL)