Skip to content

Commit

Permalink
Prefer ROM-fns over custom impl (#2462)
Browse files Browse the repository at this point in the history
* Prefer ROM-fns over custom impl

* PR number
  • Loading branch information
bjoernQ authored Nov 4, 2024
1 parent 1e6820d commit fd45da4
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 139 deletions.
9 changes: 9 additions & 0 deletions esp-hal/ld/esp32/rom/additional.ld
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ PROVIDE ( strncpy = 0x400015d4 );
PROVIDE ( strncmp = 0x4000c5f4 );

PROVIDE ( bzero = 0x4000c1f4 );

PROVIDE ( strcat = 0x4000c518 );
PROVIDE ( strcmp = 0x40001274 );
PROVIDE ( strchr = 0x4000c53c );
PROVIDE ( strlcpy = 0x4000c584 );
PROVIDE ( strstr = 0x4000c674 );
PROVIDE ( strcasecmp = 0x400011cc );
PROVIDE ( strdup = 0x4000143c );
PROVIDE ( atoi = 0x400566c4 );
9 changes: 9 additions & 0 deletions esp-hal/ld/esp32c2/rom/additional.ld
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,12 @@ memcmp = 0x40000494;
strcpy = 0x40000498;
strncpy = 0x4000049c;
strncmp = 0x400004a4;

PROVIDE ( strcat = 0x4000050c );
PROVIDE ( strcmp = 0x400004a0 );
PROVIDE ( strchr = 0x40000514 );
PROVIDE ( strlcpy = 0x40000524 );
PROVIDE ( strstr = 0x400004ac );
PROVIDE ( strcasecmp = 0x40000504 );
PROVIDE ( strdup = 0x40000510 );
PROVIDE ( atoi = 0x40000580 );
9 changes: 9 additions & 0 deletions esp-hal/ld/esp32c3/rom/additional.ld
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@ strcpy = 0x40000364;
abs = 0x40000424;

PROVIDE(cache_dbus_mmu_set = 0x40000564);

PROVIDE( strcat = 0x400003d8 );
PROVIDE( strcmp = 0x4000036c );
PROVIDE( strchr = 0x400003e0 );
PROVIDE( strlcpy = 0x400003f0 );
PROVIDE( strstr = 0x40000378 );
PROVIDE( strcasecmp = 0x400003d0 );
PROVIDE( strdup = 0x400003dc );
PROVIDE( atoi = 0x4000044c );
9 changes: 9 additions & 0 deletions esp-hal/ld/esp32c6/rom/additional.ld
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,12 @@ strncpy = 0x400004bc;
strcpy = 0x400004b8;

abs = 0x40000578;

PROVIDE(strcat = 0x4000052c);
PROVIDE(strcmp = 0x400004c0);
PROVIDE(strchr = 0x40000534);
PROVIDE(strlcpy = 0x40000544);
PROVIDE(strstr = 0x400004cc);
PROVIDE(strcasecmp = 0x40000524);
PROVIDE(strdup = 0x40000530);
PROVIDE(atoi = 0x400005a0);
9 changes: 9 additions & 0 deletions esp-hal/ld/esp32h2/rom/additional.ld
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,12 @@ strncpy = 0x400004b4;
strcpy = 0x400004b0;

abs = 0x40000570;

PROVIDE( strcat = 0x40000524 );
PROVIDE( strcmp = 0x400004b8 );
PROVIDE( strchr = 0x4000052c );
PROVIDE( strlcpy = 0x4000053c );
PROVIDE( strstr = 0x400004c4 );
PROVIDE( strcasecmp = 0x4000051c );
PROVIDE( strdup = 0x40000528 );
PROVIDE( atoi = 0x40000598 );
8 changes: 8 additions & 0 deletions esp-hal/ld/esp32s2/rom/additional.ld
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ strncmp = 0x4001ae64;
bzero = 0x400078c8;

PROVIDE ( cache_dbus_mmu_set = 0x40018eb0 );

PROVIDE ( strcat = 0x4001ad90 );
PROVIDE ( strcmp = 0x40007be4 );
PROVIDE ( strchr = 0x4001adb0 );
PROVIDE ( strlcpy = 0x4001adf8 );
PROVIDE ( strstr = 0x4001aee8 );
PROVIDE ( strcasecmp = 0x40007b38 );
PROVIDE ( strdup = 0x40007d84 );
9 changes: 9 additions & 0 deletions esp-hal/ld/esp32s3/rom/additional.ld
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,12 @@ bzero = 0x40001260;
PROVIDE(cache_dbus_mmu_set = 0x400019b0);
PROVIDE( Cache_Suspend_DCache_Autoload = 0x40001734 );
PROVIDE( Cache_Suspend_DCache = 0x400018b4 );

PROVIDE( strcat = 0x40001374 );
PROVIDE( strcmp = 0x40001230 );
PROVIDE( strchr = 0x4000138c );
PROVIDE( strlcpy = 0x400013bc );
PROVIDE( strstr = 0x40001254 );
PROVIDE( strcasecmp = 0x4000135c );
PROVIDE( strdup = 0x40001380 );
PROVIDE( atoi = 0x400014d0 );
2 changes: 2 additions & 0 deletions esp-wifi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Removed

- Feature `have-strchr` is removed (#2462)

## 0.10.1 - 2024-10-10

### Changed
Expand Down
3 changes: 0 additions & 3 deletions esp-wifi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,6 @@ log = ["dep:log", "esp-hal/log", "esp-wifi-sys/log"]
## Enable sniffer mode support
sniffer = ["wifi"]

# Don't include `strchr` - not shown in docs
have-strchr = []

# Implement serde Serialize / Deserialize
serde = ["dep:serde", "enumset?/serde", "heapless/serde"]

Expand Down
139 changes: 3 additions & 136 deletions esp-wifi/src/compat/misc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::compat::malloc::*;
// these are not called but needed for linking

#[no_mangle]
unsafe extern "C" fn fwrite(ptr: *const (), size: usize, count: usize, stream: *const ()) -> usize {
Expand All @@ -20,141 +20,8 @@ unsafe extern "C" fn fclose(stream: *const ()) -> i32 {
todo!("fclose {:?}", stream);
}

#[no_mangle]
unsafe extern "C" fn strcat(destination: *mut u8, source: *const u8) -> *const u8 {
trace!("strcat {:?} {:?}", destination, source);

let dst: *mut u8 = strchr(destination.cast(), 0) as *mut u8;
let len = strchr(source.cast(), 0) as usize - source as usize;
core::ptr::copy(source, dst, len);
destination
}

#[no_mangle]
unsafe extern "C" fn strcmp(str1: *const i8, str2: *const i8) -> i32 {
trace!("strcmp {:?} {:?}", str1, str2);

// TODO: unwrap!() when defmt supports it
let s1 = core::ffi::CStr::from_ptr(str1).to_str().unwrap();
let s2 = core::ffi::CStr::from_ptr(str2).to_str().unwrap();

let x = s1.cmp(s2);

match x {
core::cmp::Ordering::Less => -1,
core::cmp::Ordering::Equal => 0,
core::cmp::Ordering::Greater => 1,
}
}

#[cfg(feature = "have-strchr")]
extern "C" {
fn strchr(str: *const i8, c: i32) -> *const i8;
}

#[cfg(not(feature = "have-strchr"))]
#[no_mangle]
unsafe extern "C" fn strchr(str: *const i8, c: i32) -> *const i8 {
trace!("strchr {:?} {}", str, c);

unsafe {
let mut p = str;
loop {
if *p == c as i8 {
return p;
}

if *p == 0 {
return core::ptr::null();
}

p = p.add(1);
}
}
}

#[no_mangle]
unsafe extern "C" fn strlcpy(dst: *mut u8, src: *const u8, size: usize) -> usize {
trace!("strlcpy {:?} {:?} {}", dst, src, size);

let mut dst = dst;
let mut src = src;
let mut cnt = 0;
loop {
dst.write_volatile(0);

let c = src.read_volatile();

if c == 0 || cnt >= size {
break;
}

dst.write_volatile(c);
dst = dst.add(1);
src = src.add(1);
cnt += 1;
}

cnt
}

#[no_mangle]
unsafe extern "C" fn strstr(str1: *const i8, str2: *const i8) -> *const i8 {
trace!("strstr {:?} {:?}", str1, str2);

let s1 = core::ffi::CStr::from_ptr(str1).to_str().unwrap();
let s2 = core::ffi::CStr::from_ptr(str2).to_str().unwrap();

let idx = s1.find(s2);

match idx {
Some(offset) => str1.add(offset),
None => core::ptr::null(),
}
}

#[no_mangle]
unsafe extern "C" fn strcasecmp(str1: *const u8, str2: *const u8) -> i32 {
trace!("strcasecmp {:?} {:?}", str1, str2);

let mut str1 = str1;
let mut str2 = str2;

let mut c1 = *str1 as char;
let mut c2 = *str2 as char;

while c1 != '\0' && c2 != '\0' {
c1 = c1.to_ascii_lowercase();
c2 = c2.to_ascii_lowercase();

if c1 != c2 {
return c1 as i32 - c2 as i32;
}

str1 = str1.add(1);
str2 = str2.add(1);

c1 = *str1 as char;
c2 = *str2 as char;
}

c1 as i32 - c2 as i32
}

#[no_mangle]
unsafe extern "C" fn strdup(str: *const i8) -> *const u8 {
trace!("strdup {:?}", str);

unsafe {
let s = core::ffi::CStr::from_ptr(str);
let s = s.to_str().unwrap(); // TODO when defmt supports it

let p = malloc(s.len() + 1);
core::ptr::copy_nonoverlapping(str, p as *mut i8, s.len() + 1);
p as *const u8
}
}

// not available in ROM on ESP32-S2
#[cfg(feature = "esp32s2")]
#[no_mangle]
unsafe extern "C" fn atoi(str: *const i8) -> i32 {
trace!("atoi {:?}", str);
Expand Down

0 comments on commit fd45da4

Please sign in to comment.