Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
add many more commands and restructed them into bundles
Browse files Browse the repository at this point in the history
  • Loading branch information
john-tornblom committed Mar 2, 2024
1 parent 826c75c commit d769584
Show file tree
Hide file tree
Showing 49 changed files with 4,506 additions and 105 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,4 +1,5 @@
*~
*.o
*.elf
sh_elf.c
*.elf.inc

20 changes: 16 additions & 4 deletions Makefile
Expand Up @@ -26,27 +26,39 @@ endif
CFLAGS := -Wall
LDADD := -lSceLibcInternal -lkernel_sys

SUBDIRS := bundles/core bundles/http2_get

TOPTARGETS := all clean

$(TOPTARGETS): $(SUBDIRS)

$(SUBDIRS):
make -C $@ $(MAKECMDGOALS)

.PHONY: $(TOPTARGETS) $(SUBDIRS)


all: shsrv.elf sh.elf

shsrv.o: sh_elf.c
shsrv.o: sh.elf.inc

bundle.o: commands/env_elf.c commands/ls_elf.c commands/ps_elf.c
builtin.o: bundles/core/core.elf.inc bundles/http2_get/http2_get.elf.inc

%.o: %.c
$(CC) -c $(CFLAGS) -o $@ $<

shsrv.elf: shsrv.o elfldr.o pt.o
$(LD) -o $@ $^ $(LDADD)

sh.elf: sh.o builtin.o bundle.o elfldr.o pt.o
sh.elf: sh.o builtin.o elfldr.o pt.o
$(LD) -o $@ $^ $(LDADD)

sh_elf.c: sh.elf
sh.elf.inc: sh.elf
xxd -i $^ > $@

clean:
rm -f *.o *.elf

test: shsrv.elf
nc -q0 $(PS5_HOST) $(PS5_PORT) < $^

2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -44,7 +44,7 @@ PATH enviroment variable, which is initialized to /data/hbroot/bin and
/mnt/usb0/hbroot/bin

```console
john@localhost:tmp$ wget https://github.com/john-tornblom/ps5-payload-sdk/releases/download/releases%2Fv0.6/Payload.binaries.zip
john@localhost:tmp$ wget https://github.com/john-tornblom/ps5-payload-sdk/releases/download/releases%2Fv0.9/Payload.binaries.zip
john@localhost:tmp$ unzip Payload.binaries.zip samples/hello_sprx/hello_sprx.elf
john@localhost:tmp$ curl -T samples/hello_sprx/hello_sprx.elf ftp://ps5:2121/data/hbroot/bin/
john@localhost:tmp$ echo "hello_sprx.elf" | nc -q0 $PS5_HOST 2323
Expand Down
146 changes: 136 additions & 10 deletions builtin.c
Expand Up @@ -24,11 +24,13 @@ along with this program; see the file COPYING. If not, see
#include <unistd.h>

#include <sys/stat.h>

#include <ps5/kernel.h>

#include "builtin.h"

#include "bundles/core/core.elf.inc"
#include "bundles/http2_get/http2_get.elf.inc"


#define ispathsep(ch) ((ch) == '/' || (ch) == '\\')
#define iseos(ch) ((ch) == '\0')
Expand All @@ -38,10 +40,22 @@ along with this program; see the file COPYING. If not, see
/**
* Map names of builtin commands.
**/
typedef struct builtin_map {
typedef struct builtin_cmd_map {
const char *name;
builtin_cmd_t *cmd;
} builtin_map_t;
} builtin_cmd_map_t;


/**
* Map names of builtin ELFs.
**/
typedef struct builtin_elf_map {
const char *name;
unsigned char *elf;
} builtin_elf_map_t;


static int main_help(int argc, char **argv);


/**
Expand Down Expand Up @@ -245,7 +259,7 @@ main_export(int argc, char **argv) {
char *name;
char *val;
char *sep;

if(argc < 2 || !(sep=strstr(argv[1], "="))) {
printf("usage: %s NAME=value\n", argv[0]);
return EXIT_FAILURE;
Expand All @@ -259,7 +273,7 @@ main_export(int argc, char **argv) {
perror(argv[0]);
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}

Expand All @@ -270,9 +284,10 @@ main_export(int argc, char **argv) {
static int
main_exec(int argc, char** argv) {
if(argc <= 1) {
fprintf(stderr, "%s: missing operand\n", argv[0]);
return EXIT_FAILURE;
}

argv[argc] = NULL;
execvp(argv[1], (char **) argv + 1);
perror(argv[1]);
Expand All @@ -281,25 +296,136 @@ main_exec(int argc, char** argv) {
}


/**
*
**/
static int
main_sleep(int argc, char **argv) {
if(argc <= 1) {
fprintf(stderr, "%s: missing operand\n", argv[0]);
return -1;
}

unsigned int seconds = atoi(argv[1]);
sleep(seconds);

return 0;
}


/**
* Lookup table for builtin commands.
**/
static builtin_map_t map[] = {
static builtin_cmd_map_t cmd_map[] = {
{"cd", main_cd},
{"chroot", main_chroot},
{"exec", main_exec},
{"exit", main_exit},
{"export", main_export},
{"help", main_help},
{"sleep", main_sleep},
};


/**
* Lookup table for builtin ELFs.
**/
static builtin_elf_map_t elf_map[] = {
{"cat", core_elf},
{"chgrp", core_elf},
{"chmod", core_elf},
{"chown", core_elf},
{"cmp", core_elf},
{"cp", core_elf},
{"echo", core_elf},
{"env", core_elf},
{"file", core_elf},
{"find", core_elf},
{"grep", core_elf},
{"hexdump", core_elf},
{"id", core_elf},
{"kill", core_elf},
{"ln", core_elf},
{"ls", core_elf},
{"mkdir", core_elf},
{"mknod", core_elf},
{"mount", core_elf},
{"mv", core_elf},
{"notify", core_elf},
{"ps", core_elf},
{"pwd", core_elf},
{"rm", core_elf},
{"rmdir", core_elf},
{"sfocreate", core_elf},
{"sfoinfo", core_elf},
{"stat", core_elf},
{"sum", core_elf},
{"sync", core_elf},
{"sysctl", core_elf},
{"touch", core_elf},
{"umount", core_elf},

{"http2_get", http2_get_elf},
};


static int
qsort_cmp_names(const void *a, const void *b) {
return strcmp(*(const char **)a, *(const char **)b);
}


/**
* Print a list of available commands to stdout.
**/
static int
main_help(int argc, char **argv) {
size_t cmd_map_len = (sizeof(cmd_map)/sizeof(cmd_map[0]));
size_t elf_map_len = (sizeof(elf_map)/sizeof(elf_map[0]));
size_t n = cmd_map_len + elf_map_len;
const char* names[n];

for(size_t i=0; i<cmd_map_len; i++) {
names[i] = cmd_map[i].name;
}
for(size_t i=0; i<elf_map_len; i++) {
names[cmd_map_len+i] = elf_map[i].name;
}

qsort(names, n, sizeof(const char*), qsort_cmp_names);

printf("Builtin commands:\n");
for(size_t i=0; i<n; i++) {
printf(" %s\n", names[i]);
}

return 0;
}


builtin_cmd_t*
builtin_find_cmd(const char* name) {
for(int i=0; i<sizeof(map)/sizeof(map[0]); i++) {
if(!strcmp(name, map[i].name)) {
return map[i].cmd;
size_t n = (sizeof(cmd_map)/sizeof(cmd_map[0]));

for(size_t i=0; i<n; i++) {
if(!strcmp(name, cmd_map[i].name)) {
return cmd_map[i].cmd;
}
}

return NULL;
}


uint8_t*
builtin_find_elf(const char* name) {
size_t n = sizeof(elf_map)/sizeof(elf_map[0]);

for(size_t i=0; i<n; i++) {
if(!strcmp(name, elf_map[i].name)) {
return elf_map[i].elf;
}
}

return NULL;
}
7 changes: 7 additions & 0 deletions builtin.h
Expand Up @@ -16,6 +16,8 @@ along with this program; see the file COPYING. If not, see

#pragma once

#include <stdint.h>


/**
* Prototype for builtin commands.
Expand All @@ -28,3 +30,8 @@ typedef int (builtin_cmd_t)(int argc, char **argv);
**/
builtin_cmd_t* builtin_find_cmd(const char* name);


/**
* Find a builtin ELF by its name.
**/
uint8_t* builtin_find_elf(const char* name);
19 changes: 10 additions & 9 deletions commands/Makefile → bundles/core/Makefile
Expand Up @@ -23,20 +23,21 @@ endif
CFLAGS += -Wall
LDADD += -lkernel_sys

SRCS = env_elf.c ls_elf.c ps_elf.c
SRCS := $(wildcard *.c)
OBJS := $(SRCS:.c=.o)

all: $(SRCS)
all: core.elf.inc

%.o: %.c
$(CC) -c $(CFLAGS) -o $@ $<
core.elf.inc: core.elf
xxd -i $^ > $@

%.elf: %.o
core.elf: $(OBJS)
$(LD) -o $@ $^ $(LDADD)

%_elf.c: %.elf
xxd -i $^ > $@

clean:
rm -f *.o *.elf *_elf.c
rm -f *.o *.elf *.elf.c

%.o: %.c
$(CC) -c $(CFLAGS) -o $@ $<

test:

0 comments on commit d769584

Please sign in to comment.