Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
807 changes: 807 additions & 0 deletions .clang-format

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions .clang-format-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/ccan/*
21 changes: 21 additions & 0 deletions .github/workflows/github-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@ name: CI
on: [push, pull_request]

jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- name: Install dependencies
run: sudo apt-get update -qq &&
sudo apt-get install -qq
ninja-build python3-pip
device-tree-compiler
- name: Install meson
run: pip3 install --user meson
- name: Install clang
uses: KyleMayes/install-llvm-action@v2.0.9
with:
version: "21"
- name: Check clang-format
run: meson setup build-lint && ninja -C build-lint clang-format-check
- name: Check meson format
run: meson format --check-only --recursive

build:
name: Build
runs-on: ubuntu-latest
Expand Down
36 changes: 20 additions & 16 deletions meson.build
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
project('culvert', 'c',
default_options: [
'buildtype=debugoptimized',
'default_library=static',
'warning_level=2',
'c_std=gnu99',
'b_lto=true',
'b_ndebug=true',
],
meson_version: '>=0.63.0',
version: 'v0.4.0')
project(
'culvert',
'c',
default_options: [
'buildtype=debugoptimized',
'default_library=static',
'warning_level=2',
'c_std=gnu99',
'b_lto=true',
'b_ndebug=true',
],
meson_version: '>=0.63.0',
version: 'v0.4.0',
)

add_global_link_arguments('-z', 'noexecstack', language: 'c')

libfdt_dep = dependency('libfdt',
default_options: [ 'warning_level=1',
'python=disabled' ],
fallback: [ 'dtc', 'libfdt_dep' ],
required: true)
libfdt_dep = dependency(
'libfdt',
default_options: ['warning_level=1', 'python=disabled'],
fallback: ['dtc', 'libfdt_dep'],
required: true,
)

subdir('src')
138 changes: 76 additions & 62 deletions src/ahb.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,84 +17,98 @@

ssize_t ahb_siphon_out(struct ahb *ctx, uint32_t phys, ssize_t len, int outfd)
{
ssize_t ingress, egress;
void *chunk, *cursor;
int rc = 0;

if (!len)
return 0;

chunk = malloc(AHB_CHUNK);
if (!chunk)
return -errno;

do {
ingress = (len > AHB_CHUNK || len == -1) ? AHB_CHUNK : len;

ingress = ahb_read(ctx, phys, chunk, ingress);
if (ingress < 0) { rc = -EIO; goto done; }

phys += ingress;
if (len > 0) {
len -= ingress;
}

cursor = chunk;
while (ingress) {
egress = write(outfd, cursor, ingress);
if (egress == -1) { rc = -errno; goto done; }

cursor += egress;
ingress -= egress;
}

fprintf(stderr, ".");
} while (!!len);
ssize_t ingress, egress;
void *chunk, *cursor;
int rc = 0;

if (!len)
return 0;

chunk = malloc(AHB_CHUNK);
if (!chunk)
return -errno;

do {
ingress = (len > AHB_CHUNK || len == -1) ? AHB_CHUNK : len;

ingress = ahb_read(ctx, phys, chunk, ingress);
if (ingress < 0) {
rc = -EIO;
goto done;
}

phys += ingress;
if (len > 0) {
len -= ingress;
}

cursor = chunk;
while (ingress) {
egress = write(outfd, cursor, ingress);
if (egress == -1) {
rc = -errno;
goto done;
}

cursor += egress;
ingress -= egress;
}

fprintf(stderr, ".");
} while (!!len);

done:
fprintf(stderr, "\n");
free(chunk);
fprintf(stderr, "\n");
free(chunk);

return rc;
return rc;
}

ssize_t ahb_siphon_in(struct ahb *ctx, uint32_t phys, ssize_t len, int infd)
{
ssize_t ingress, egress;
void *chunk;
int rc = 0;

chunk = malloc(AHB_CHUNK);
if (!chunk)
return -1;

while ((ingress = read(infd, chunk, (len > AHB_CHUNK || len == -1) ? AHB_CHUNK : len))) {
if (ingress < 0) { rc = -errno; goto done; }

egress = ahb_write(ctx, phys, chunk, ingress);
if (egress < 0) { rc = -EIO; goto done; }

phys += ingress;
if (len > 0) {
len -= ingress;
}

fprintf(stderr, ".");
}
ssize_t ingress, egress;
void *chunk;
int rc = 0;

chunk = malloc(AHB_CHUNK);
if (!chunk)
return -1;

while ((ingress = read(infd, chunk,
(len > AHB_CHUNK || len == -1) ? AHB_CHUNK :
len))) {
if (ingress < 0) {
rc = -errno;
goto done;
}

egress = ahb_write(ctx, phys, chunk, ingress);
if (egress < 0) {
rc = -EIO;
goto done;
}

phys += ingress;
if (len > 0) {
len -= ingress;
}

fprintf(stderr, ".");
}

done:
fprintf(stderr, "\n");
free(chunk);
fprintf(stderr, "\n");
free(chunk);

return rc;
return rc;
}

int ahb_release_bridge(struct ahb *ctx)
{
return ctx->drv->release ? ctx->drv->release(ctx) : 0;
return ctx->drv->release ? ctx->drv->release(ctx) : 0;
}

int ahb_reinit_bridge(struct ahb *ctx)
{
return ctx->drv->reinit ? ctx->drv->reinit(ctx) : 0;
return ctx->drv->reinit ? ctx->drv->reinit(ctx) : 0;
}
63 changes: 34 additions & 29 deletions src/ahb.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,64 +14,69 @@
#include <sys/types.h>

struct ahb_range {
const char *name;
uint32_t start;
uint64_t len;
bool rw;
const char *name;
uint32_t start;
uint64_t len;
bool rw;
};

struct ahb;

struct ahb_ops {
ssize_t (*read)(struct ahb *ctx, uint32_t phys, void *buf, size_t len);
ssize_t (*write)(struct ahb *ctx, uint32_t phys, const void *buf, size_t len);
int (*readl)(struct ahb *ctx, uint32_t phys, uint32_t *val);
int (*writel)(struct ahb *ctx, uint32_t phys, uint32_t val);
ssize_t (*read)(struct ahb *ctx, uint32_t phys, void *buf, size_t len);
ssize_t (*write)(struct ahb *ctx, uint32_t phys, const void *buf,
size_t len);
int (*readl)(struct ahb *ctx, uint32_t phys, uint32_t *val);
int (*writel)(struct ahb *ctx, uint32_t phys, uint32_t val);
};

struct ahb {
const struct bridge_driver *drv;
const struct ahb_ops *ops;
const struct bridge_driver *drv;
const struct ahb_ops *ops;
};

static inline void ahb_init_ops(struct ahb *ctx, const struct bridge_driver *drv,
const struct ahb_ops *ops)
static inline void ahb_init_ops(struct ahb *ctx,
const struct bridge_driver *drv,
const struct ahb_ops *ops)
{
ctx->drv = drv;
ctx->ops = ops;
ctx->drv = drv;
ctx->ops = ops;
}

static inline ssize_t ahb_read(struct ahb *ctx, uint32_t phys, void *buf, size_t len)
static inline ssize_t ahb_read(struct ahb *ctx, uint32_t phys, void *buf,
size_t len)
{
return ctx->ops->read(ctx, phys, buf, len);
return ctx->ops->read(ctx, phys, buf, len);
}

static inline ssize_t ahb_write(struct ahb *ctx, uint32_t phys, const void *buf, size_t len)
static inline ssize_t ahb_write(struct ahb *ctx, uint32_t phys, const void *buf,
size_t len)
{
return ctx->ops->write(ctx, phys, buf, len);
return ctx->ops->write(ctx, phys, buf, len);
}

static inline int ahb_readl(struct ahb *ctx, uint32_t phys, uint32_t *val)
{
int rc = ctx->ops->readl(ctx, phys, val);
if (!rc) {
logt("%s: 0x%08"PRIx32": 0x%08"PRIx32"\n", __func__, phys, *val);
}
int rc = ctx->ops->readl(ctx, phys, val);
if (!rc) {
logt("%s: 0x%08" PRIx32 ": 0x%08" PRIx32 "\n", __func__, phys,
*val);
}

return rc;
return rc;
}

static inline int ahb_writel(struct ahb *ctx, uint32_t phys, uint32_t val)
{
int rc = ctx->ops->writel(ctx, phys, val);
if (!rc) {
logt("%s: 0x%08"PRIx32": 0x%08"PRIx32"\n", __func__, phys, val);
}
int rc = ctx->ops->writel(ctx, phys, val);
if (!rc) {
logt("%s: 0x%08" PRIx32 ": 0x%08" PRIx32 "\n", __func__, phys,
val);
}

return rc;
return rc;
}


ssize_t ahb_siphon_out(struct ahb *ctx, uint32_t phys, ssize_t len, int outfd);
ssize_t ahb_siphon_in(struct ahb *ctx, uint32_t phys, ssize_t len, int infd);

Expand Down
Loading
Loading