Skip to content

Fix PXI I2C size decode - #976

Open
rosaage wants to merge 3 commits into
d0k3:masterfrom
rosaage:master
Open

Fix PXI I2C size decode#976
rosaage wants to merge 3 commits into
d0k3:masterfrom
rosaage:master

Conversation

@rosaage

@rosaage rosaage commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

During some testing I discovered changing SHMEM_BUFFER_SIZE from 2048 to 20480 broke I2C transfers.
This was due to size not being calculated correctly.

Test code:

This was tested using onlinegdb.com:

#include <stdio.h>
#include <stdint.h>

#define SHMEM_BUFFER_SIZE 20480
#define BIT(x) (1 << (x))

int main()
{
  uint32_t var = 0x01FF;
  var = var | (22 << 16); // "size" sent from arm9
  var = var | BIT(31); // Write operation
  uint32_t old_size = (var >> 16) % SHMEM_BUFFER_SIZE;
  uint32_t new_size = ((var >> 16) & 0x7FFF) % SHMEM_BUFFER_SIZE;

  printf("Old size: %01d, New size: %01d", old_size, new_size);

  return 0;
}

Output of the test code with size set to 22:
Old size: 12310, New size: 22

The second commit adds a 15-bit size check to the arm9 side, rejecting commands early if size is too big.

Comment thread arm9/source/system/i2c.c Outdated
Comment on lines +12 to +15
if (size >= SHMEM_BUFFER_SIZE)
return false;
if (size > 0x7FFF)
return false;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather see something like _Static_assert(SHMEM_BUFFER_SIZE < BIT(15)); rather than the two runtime checks here, but other than that it looks perfect

the "proper" way to fix it would be to define some types in shmem.h that describe the bitfield layout but that might be a bit too many changes, I'd rather have this as it's only used once or twice

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this potentially not work correctly?
if SHMEM_BUFFER_SIZE is 2048 (current value) and you try to read/write 4096 bytes, then nothing will stop that with a static assert of SHMEM_BUFFER_SIZE < BIT(31)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants