Add cross-platform Windows and Linux CI builds
Introduce a reproducible CMake build and GitHub Actions matrix for Ubuntu Linux x64 and Windows x64. Each platform installs libusb, builds the usbboot executable, and publishes a platform-specific artifact containing the executable, required boot binaries, and documentation. The Windows artifact also includes libusb-1.0.dll. Add a small Windows getopt_long compatibility layer because the Microsoft CRT does not provide the POSIX GNU option parser used by usbboot. Add Windows sleep support and make the source explicitly include the fixed-width integer and time headers needed by the cross-platform build. Replace C99 variable-length arrays with checked heap allocations so the source compiles with MSVC, which does not support VLAs. Cast libusb request-type flags to avoid MSVC enum warnings, and use a conforming cleanup(void) callback signature for atexit(). Correct the existing cleanup NULL check so an open libusb handle is actually closed. Document local CMake builds, CI artifacts, the Windows libusb DLL, and the requirement to bind the boot-mode device to WinUSB before use. Credit: OpenAI Codex (GPT-5), collaborating with dark98 on the Windows port and CI implementation.
This commit is contained in:
@@ -23,13 +23,21 @@
|
||||
****************************************************************************/
|
||||
|
||||
#include <libusb.h>
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include "getopt_compat.h"
|
||||
#define sleep(seconds) Sleep((DWORD)((seconds) * 1000UL))
|
||||
#else
|
||||
#include <getopt.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <time.h>
|
||||
|
||||
#define VR_GET_CPU_INFO 0
|
||||
#define VR_SET_DATA_ADDRESS 1
|
||||
@@ -42,6 +50,8 @@
|
||||
#define VR_WRITE 0x12
|
||||
#define VR_READ 0x13
|
||||
#define VR_UPDATE_CFG 0x14
|
||||
#define USB_VENDOR_IN ((uint8_t)((uint8_t)LIBUSB_ENDPOINT_IN | (uint8_t)LIBUSB_REQUEST_TYPE_VENDOR | (uint8_t)LIBUSB_RECIPIENT_DEVICE))
|
||||
#define USB_VENDOR_OUT ((uint8_t)((uint8_t)LIBUSB_ENDPOINT_OUT | (uint8_t)LIBUSB_REQUEST_TYPE_VENDOR | (uint8_t)LIBUSB_RECIPIENT_DEVICE))
|
||||
|
||||
#define MAGIC_DEBUG ('D' << 24) | ('B' << 16) | ('G' << 8) | 0
|
||||
#define MAGIC_MMC ('M' << 24) | ('M' << 16) | ('C' << 8) | 0
|
||||
@@ -175,7 +185,7 @@ void jz_get_cpu_info(void)
|
||||
|
||||
uint8_t buf[9];
|
||||
int ret = libusb_control_transfer(g_usb_dev,
|
||||
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
|
||||
USB_VENDOR_IN,
|
||||
VR_GET_CPU_INFO, 0, 0, buf, 8, 1000);
|
||||
if(ret != 0)
|
||||
die("Can't get CPU info: %d", ret);
|
||||
@@ -227,8 +237,8 @@ void bulk_transfer_out(void* data, int length) {
|
||||
void name(unsigned long param) { \
|
||||
ensure_usb(); \
|
||||
verbose("Issue " #type fmt, param); \
|
||||
int ret = libusb_control_transfer(g_usb_dev, \
|
||||
LIBUSB_ENDPOINT_OUT|LIBUSB_REQUEST_TYPE_VENDOR|LIBUSB_RECIPIENT_DEVICE, \
|
||||
int ret = libusb_control_transfer(g_usb_dev, \
|
||||
USB_VENDOR_OUT, \
|
||||
VR_##type, param >> 16, param & 0xffff, NULL, 0, 1000); \
|
||||
if(ret != 0) \
|
||||
die("Request " #type " failed: %d", ret); \
|
||||
@@ -249,7 +259,7 @@ void jz_generic_out(uint8_t op,
|
||||
ensure_usb();
|
||||
verbose("Issue 0x%x", op);
|
||||
int ret = libusb_control_transfer(g_usb_dev,
|
||||
LIBUSB_ENDPOINT_OUT|LIBUSB_REQUEST_TYPE_VENDOR|LIBUSB_RECIPIENT_DEVICE,
|
||||
USB_VENDOR_OUT,
|
||||
op, param >> 16, param & 0xffff, data, len, 1000);
|
||||
if(ret != len)
|
||||
die("Request 0x%x failed, only transfered: %d", op, ret);
|
||||
@@ -282,7 +292,7 @@ void jz_get_ack() {
|
||||
|
||||
uint8_t buf[4];
|
||||
int ret = libusb_control_transfer(g_usb_dev,
|
||||
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
|
||||
USB_VENDOR_IN,
|
||||
VR_GET_ACK, 0, 0, buf, 4, 1000);
|
||||
if(ret != 4)
|
||||
die("Can't get ACK: %d", ret);
|
||||
@@ -314,7 +324,9 @@ void enable_mmc() {
|
||||
memset(mmc_param_info->data, 0, sizeof(mmc_param));
|
||||
|
||||
uint32_t data_size = 3 * 8 + policy_param_info->size + dbg_param_info->size + mmc_param_info->size;
|
||||
unsigned char data[data_size];
|
||||
unsigned char *data = (unsigned char*)malloc(data_size);
|
||||
if(data == NULL)
|
||||
die("Can't allocate %u bytes for MMC configuration", data_size);
|
||||
unsigned char *p = data;
|
||||
uint32_t offset = 0;
|
||||
memcpy(p + offset, dbg_param_info, 4 + 4 + dbg_param_info->size);
|
||||
@@ -335,6 +347,7 @@ void enable_mmc() {
|
||||
free(dbg_param_info);
|
||||
free(mmc_param_info);
|
||||
free(update);
|
||||
free(data);
|
||||
|
||||
jz_get_ack();
|
||||
jz_init(0);
|
||||
@@ -371,11 +384,16 @@ void mmc_read_partition(uint32_t offset, uint32_t length, const char* fname) {
|
||||
printf("dumping parition at offset 0x%x, size 0x%x\n", offset, length);
|
||||
|
||||
uint32_t chunk_size = 1024 * 1024 * 2; // 2mb
|
||||
unsigned char chunk[chunk_size];
|
||||
|
||||
FILE* f = fopen(fname, "wb");
|
||||
if(f == NULL)
|
||||
die("Can't open file '%s' for writing", fname);
|
||||
|
||||
unsigned char *chunk = (unsigned char*)malloc(chunk_size);
|
||||
if(chunk == NULL) {
|
||||
fclose(f);
|
||||
die("Can't allocate %u bytes for partition dump", chunk_size);
|
||||
}
|
||||
|
||||
uint32_t cursor = offset;
|
||||
uint32_t end = offset + length;
|
||||
@@ -399,6 +417,7 @@ void mmc_read_partition(uint32_t offset, uint32_t length, const char* fname) {
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
free(chunk);
|
||||
}
|
||||
|
||||
void mmc_write(uint32_t offset, uint32_t length, unsigned char* in) {
|
||||
@@ -418,12 +437,14 @@ void swap_ota_partition(bool force) {
|
||||
ensure_usb();
|
||||
enable_mmc();
|
||||
|
||||
uint32_t ota_len = 512;
|
||||
unsigned char ota[ota_len];
|
||||
const uint32_t ota_len = 512;
|
||||
unsigned char *ota = (unsigned char*)malloc(ota_len);
|
||||
char *ota_in = (char*)malloc(ota_len);
|
||||
if(ota == NULL || ota_in == NULL)
|
||||
die("Can't allocate OTA buffers");
|
||||
|
||||
mmc_read(0x100000, ota_len, ota);
|
||||
|
||||
char ota_in[ota_len];
|
||||
memset(ota_in, 0, ota_len);
|
||||
|
||||
if (strncmp((char*)ota, "ota:kernel2", 11) == 0) {
|
||||
@@ -443,6 +464,8 @@ void swap_ota_partition(bool force) {
|
||||
|
||||
mmc_write(0x100000, ota_len, (unsigned char*)ota_in);
|
||||
printf("Switched OTA to %s", ota_in);
|
||||
free(ota);
|
||||
free(ota_in);
|
||||
}
|
||||
|
||||
/* Default settings */
|
||||
@@ -552,9 +575,9 @@ Known CPU types and default stage1/stage2 binary settings:\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void cleanup()
|
||||
void cleanup(void)
|
||||
{
|
||||
if(g_usb_dev == NULL)
|
||||
if(g_usb_dev != NULL)
|
||||
libusb_close(g_usb_dev);
|
||||
libusb_exit(NULL);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user