Skip to content

Commit

Permalink
rpipico: fix unintended collateral damage
Browse files Browse the repository at this point in the history
Split the GPIO stuff off as picogpio, restore picoctl and fix a bug
in both with the open check.
  • Loading branch information
EtchedPixels committed Jul 14, 2024
1 parent 07b98cb commit 47ee7b6
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 58 deletions.
2 changes: 1 addition & 1 deletion Kernel/platform/platform-rpipico/utils/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ USERCPU=armm0
VERBOSE=1
include $(FUZIX_ROOT)/Target/rules.$(USERCPU)

UTILS = picoctl.c
UTILS = picoctl.c picogpio.c
UTILSBIN = $(UTILS:.c=)

.PHONY: all clean
Expand Down
79 changes: 22 additions & 57 deletions Kernel/platform/platform-rpipico/utils/picoctl.c
Original file line number Diff line number Diff line change
@@ -1,66 +1,31 @@
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>

#include <sys/gpio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include "../pico_ioctl.h"

int is_valid_uint8(const char *str) {
char *endptr;
long val = strtol(str, &endptr, 10);

// check string was a number and within the range of uint8_t
if (*endptr == '\0' && val >= 0 && val <= UINT8_MAX) {
return 1;
int main(int argc, char **argv)
{
if (argc == 1 || strcmp(argv[1], "--help") == 0)
{
puts("usage: picoctl [ --help ] <commmand>");
puts("Command list:");
puts("\tflash\tReset into flash mode.");
return 0;
}
int fd = open("/dev/sys", O_RDWR, 0);
if (fd == -1)
{
perror("Failed to open /dev/sys");
exit(1);
}
if (ioctl(fd, PICOIOC_FLASH) != 0)
{
perror("Failed to perform operation");
close(fd);
exit(1);
}
return 0;
}

int main(int argc, char **argv) {
if (argc == 1 || strcmp(argv[1], "--help") == 0)
{
puts("Turn on/off GPIO pins on the pico");
puts("Usage: picoctl <PIN> <VALUE>");
return 0;
}

if (argc != 3) {
fprintf(stderr, "Usage: %s <pin> <value>\n", argv[0]);
return 1;
}

if (!is_valid_uint8(argv[1])) {
fprintf(stderr, "Error: <pin> must be a valid uint8_t value\n");
return 1;
}
uint8_t pin = (uint8_t)strtoul(argv[1], NULL, 10);

if (!is_valid_uint8(argv[2])) {
fprintf(stderr, "Error: <value> must be a valid uint8_t value\n");
return 1;
}
uint8_t value = (uint8_t)strtoul(argv[2], NULL, 10);

int fd = open("/dev/gpio", O_RDWR, 0);
if (!fd) {
puts("Failed to open /dev/gpio.");
exit(1);
}

struct gpioreq gr;
gr.pin = pin;
gr.val = value;

if (ioctl(fd, GPIOC_SET, &gr) != 0) {
puts("Failed to perform operation.");
close(fd);
exit(1);
}

close(fd);
return 0;
return 0;
}
66 changes: 66 additions & 0 deletions Kernel/platform/platform-rpipico/utils/picogpio.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>

#include <sys/gpio.h>
#include <sys/ioctl.h>
#include "../pico_ioctl.h"

int is_valid_uint8(const char *str) {
char *endptr;
long val = strtol(str, &endptr, 10);

// check string was a number and within the range of uint8_t
if (*endptr == '\0' && val >= 0 && val <= UINT8_MAX) {
return 1;
}
return 0;
}

int main(int argc, char **argv) {
if (argc == 1 || strcmp(argv[1], "--help") == 0)
{
puts("Turn on/off GPIO pins on the pico");
puts("Usage: picogpio <PIN> <VALUE>");
return 0;
}

if (argc != 3) {
fprintf(stderr, "Usage: %s <pin> <value>\n", argv[0]);
return 1;
}

if (!is_valid_uint8(argv[1])) {
fprintf(stderr, "Error: <pin> must be a valid uint8_t value\n");
return 1;
}
uint8_t pin = (uint8_t)strtoul(argv[1], NULL, 10);

if (!is_valid_uint8(argv[2])) {
fprintf(stderr, "Error: <value> must be a valid uint8_t value\n");
return 1;
}
uint8_t value = (uint8_t)strtoul(argv[2], NULL, 10);

int fd = open("/dev/gpio", O_RDWR, 0);
if (fd == -1) {
perror("Failed to open /dev/gpio");
exit(1);
}

struct gpioreq gr;
gr.pin = pin;
gr.val = value;

if (ioctl(fd, GPIOC_SET, &gr) != 0) {
perror("Failed to perform operation");
close(fd);
exit(1);
}

close(fd);
return 0;
}

0 comments on commit 47ee7b6

Please sign in to comment.