diff --git a/include/abuf.h b/include/abuf.h index 7872e9c9b27..804ab0e3e57 100644 --- a/include/abuf.h +++ b/include/abuf.h @@ -202,6 +202,18 @@ void abuf_init_set(struct abuf *abuf, void *data, size_t size); */ void abuf_init_const(struct abuf *abuf, const void *data, size_t size); +/** + * abuf_init_const_addr() - Set up a new buffer at a given address + * + * This is similar to abuf_init_const() except that it takes an address instead + * of a pointer. Note that the abuf is unallocated. + * + * @abuf: abuf to set up + * @addr: Address to use + * @size: Size of buffer + */ +void abuf_init_const_addr(struct abuf *abuf, ulong addr, size_t size); + /** * abuf_init_size() - Set up an allocated abuf * diff --git a/lib/abuf.c b/lib/abuf.c index 3a2fd1782e9..440f6a40023 100644 --- a/lib/abuf.c +++ b/lib/abuf.c @@ -183,6 +183,13 @@ void abuf_init_const(struct abuf *abuf, const void *data, size_t size) abuf_init_set(abuf, (void *)data, size); } +#ifndef USE_HOSTCC +void abuf_init_const_addr(struct abuf *abuf, ulong addr, size_t size) +{ + return abuf_init_const(abuf, map_sysmem(addr, size), size); +} +#endif + void abuf_init_move(struct abuf *abuf, void *data, size_t size) { abuf_init_set(abuf, data, size); diff --git a/test/lib/abuf.c b/test/lib/abuf.c index 97b128c01c0..9cbb627d0b6 100644 --- a/test/lib/abuf.c +++ b/test/lib/abuf.c @@ -68,6 +68,28 @@ static int lib_test_abuf_init_const(struct unit_test_state *uts) } LIB_TEST(lib_test_abuf_init_const, 0); +/* Test abuf_init_const_addr() */ +static int lib_test_abuf_init_const_addr(struct unit_test_state *uts) +{ + struct abuf buf; + ulong start; + void *ptr; + + start = ut_check_free(); + + ptr = map_sysmem(0x100, 0); + + abuf_init_const_addr(&buf, 0x100, 10); + ut_asserteq_ptr(ptr, buf.data); + ut_asserteq(10, buf.size); + + /* No memory should have been allocated */ + ut_assertok(ut_check_delta(start)); + + return 0; +} +LIB_TEST(lib_test_abuf_init_const_addr, 0); + /* Test abuf_map_sysmem() and abuf_addr() */ static int lib_test_abuf_map_sysmem(struct unit_test_state *uts) {