Skip to content

armgit / PS5LM

Public Imported from GitHub
0

llama.cpp on a jailbroken PS5: run any GGUF model natively on the console, starting with Qwen 3.8

Add netprobe and the mbedTLS build for console downloads

Mert Cobanov9db968151573
diff --git a/.gitignore b/.gitignore
index 72a99d8..742a88b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -12,3 +12,4 @@ build/
models/
.DS_Store
+.wrangler/
diff --git a/probes/netprobe/Makefile b/probes/netprobe/Makefile
new file mode 100644
index 0000000..89b1e3a
--- /dev/null
+++ b/probes/netprobe/Makefile
@@ -0,0 +1,27 @@
+PS5_HOST ?= ps5
+PS5_PORT ?= 9021
+
+ifdef PS5_PAYLOAD_SDK
+ include $(PS5_PAYLOAD_SDK)/toolchain/prospero.mk
+else
+ $(error PS5_PAYLOAD_SDK is undefined: source scripts/env.sh)
+endif
+
+ELF := netprobe.elf
+
+# The PS5's CPU is an 8-core Zen 2.
+CFLAGS := -Wall -Wextra -Werror -O2 -g -march=znver2
+LDLIBS :=
+
+all: $(ELF)
+
+$(ELF): main.c
+ $(CC) $(CFLAGS) -o $@ $^ $(LDLIBS)
+
+clean:
+ rm -f $(ELF)
+
+test: $(ELF)
+ $(PS5_DEPLOY) -h $(PS5_HOST) -p $(PS5_PORT) $^
+
+.PHONY: all clean test
diff --git a/probes/netprobe/main.c b/probes/netprobe/main.c
new file mode 100644
index 0000000..c534bfb
--- /dev/null
+++ b/probes/netprobe/main.c
@@ -0,0 +1,114 @@
+/* PS5LM netprobe: can a payload reach the internet to download models?
+ *
+ * For a few hosts: resolve the name, then open a TCP connection to port 443
+ * (and 80), with timings. Separates "the DNS server does not answer for this
+ * name" from "no route out". Writes to stdout and /data/PS5LM/netprobe.txt.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+#include <arpa/inet.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/select.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <time.h>
+#include <unistd.h>
+
+static FILE *g_log;
+
+static void out(const char *fmt, ...) {
+ va_list ap;
+ va_start(ap, fmt);
+ vprintf(fmt, ap);
+ va_end(ap);
+ fflush(stdout);
+ if (g_log) {
+ va_start(ap, fmt);
+ vfprintf(g_log, fmt, ap);
+ va_end(ap);
+ fflush(g_log);
+ }
+}
+
+static double now_ms(void) {
+ struct timespec ts;
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ return (double)ts.tv_sec * 1e3 + (double)ts.tv_nsec * 1e-6;
+}
+
+/* Non-blocking connect with a 5 s timeout. */
+static int try_connect(const struct in_addr *ip, int port) {
+ const int s = socket(AF_INET, SOCK_STREAM, 0);
+ if (s < 0)
+ return -errno;
+ fcntl(s, F_SETFL, fcntl(s, F_GETFL) | O_NONBLOCK);
+ struct sockaddr_in sa;
+ memset(&sa, 0, sizeof(sa));
+ sa.sin_family = AF_INET;
+ sa.sin_port = htons((unsigned short)port);
+ sa.sin_addr = *ip;
+ int rc = connect(s, (struct sockaddr *)&sa, sizeof(sa));
+ if (rc != 0 && errno == EINPROGRESS) {
+ fd_set w;
+ FD_ZERO(&w);
+ FD_SET(s, &w);
+ struct timeval tv = {5, 0};
+ rc = select(s + 1, NULL, &w, NULL, &tv);
+ if (rc == 1) {
+ int err = 0;
+ socklen_t len = sizeof(err);
+ getsockopt(s, SOL_SOCKET, SO_ERROR, &err, &len);
+ rc = err ? -err : 0;
+ } else {
+ rc = rc == 0 ? -ETIMEDOUT : -errno;
+ }
+ } else if (rc != 0) {
+ rc = -errno;
+ }
+ close(s);
+ return rc;
+}
+
+int main(void) {
+ static const char *hosts[] = {"huggingface.co", "us.aws.cdn.hf.co", "github.com", "example.com"};
+
+ mkdir("/data/PS5LM", 0777);
+ g_log = fopen("/data/PS5LM/netprobe.txt", "w");
+ out("PS5LM netprobe\n");
+
+ for (size_t i = 0; i < sizeof(hosts) / sizeof(hosts[0]); i++) {
+ struct addrinfo hints, *res = NULL;
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = AF_INET;
+ hints.ai_socktype = SOCK_STREAM;
+ const double t0 = now_ms();
+ const int rc = getaddrinfo(hosts[i], "443", &hints, &res);
+ const double dt = now_ms() - t0;
+ if (rc != 0 || !res) {
+ out("%-18s dns failed (%d) after %.0f ms\n", hosts[i], rc, dt);
+ continue;
+ }
+ const struct in_addr ip = ((struct sockaddr_in *)res->ai_addr)->sin_addr;
+ char text[INET_ADDRSTRLEN];
+ inet_ntop(AF_INET, &ip, text, sizeof(text));
+ freeaddrinfo(res);
+
+ const double t1 = now_ms();
+ const int c443 = try_connect(&ip, 443);
+ const double d443 = now_ms() - t1;
+ const int c80 = try_connect(&ip, 80);
+ out("%-18s %-15s dns %.0f ms, tcp 443 %s (%.0f ms), tcp 80 %s\n", hosts[i], text, dt,
+ c443 == 0 ? "ok" : strerror(-c443), d443, c80 == 0 ? "ok" : strerror(-c80));
+ }
+
+ out("done\n");
+ if (g_log)
+ fclose(g_log);
+ return 0;
+}
diff --git a/scripts/build-mbedtls.sh b/scripts/build-mbedtls.sh
new file mode 100755
index 0000000..49db41b
--- /dev/null
+++ b/scripts/build-mbedtls.sh
@@ -0,0 +1,40 @@
+#!/usr/bin/env bash
+# Builds mbedTLS for the PS5 into .deps/mbedtls-ps5: the TLS stack PS5LM uses
+# to download models over HTTPS (Sony's own HTTP library fails on the console).
+set -euo pipefail
+
+VERSION=3.6.7
+SHA256=a7e8bcbec0e6f761b4af24f25677626b35f762f68eef79c08677a363212d11f6
+
+root="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
+# shellcheck source=env.sh
+source "$root/scripts/env.sh"
+deps="$root/.deps"
+tarball="$deps/mbedtls-$VERSION.tar.bz2"
+src="$deps/mbedtls-$VERSION"
+out="$deps/mbedtls-ps5"
+
+if [ ! -f "$tarball" ]; then
+ curl -fsSL -o "$tarball" \
+ "https://github.com/Mbed-TLS/mbedtls/releases/download/mbedtls-$VERSION/mbedtls-$VERSION.tar.bz2"
+fi
+if command -v sha256sum >/dev/null; then
+ actual=$(sha256sum "$tarball" | cut -d' ' -f1)
+else
+ actual=$(shasum -a 256 "$tarball" | cut -d' ' -f1)
+fi
+[ "$actual" = "$SHA256" ] || { echo "build-mbedtls: checksum mismatch" >&2; exit 1; }
+[ -d "$src" ] || tar xjf "$tarball" -C "$deps"
+
+"$PS5_PAYLOAD_SDK/bin/prospero-cmake" -S "$src" -B "$deps/mbedtls-build" -G Ninja \
+ -DCMAKE_BUILD_TYPE=Release \
+ -DCMAKE_INSTALL_PREFIX="$out" \
+ -DENABLE_PROGRAMS=OFF \
+ -DENABLE_TESTING=OFF \
+ -DUSE_SHARED_MBEDTLS_LIBRARY=OFF \
+ -DUSE_STATIC_MBEDTLS_LIBRARY=ON \
+ -DMBEDTLS_FATAL_WARNINGS=OFF \
+ -DCMAKE_VERBOSE_MAKEFILE=OFF
+cmake --build "$deps/mbedtls-build"
+cmake --install "$deps/mbedtls-build" > /dev/null
+echo "build-mbedtls: mbedTLS $VERSION in $out"