Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EraseFlash fails if address exceeds 0xFFFF #5

Closed
bessman opened this issue Dec 23, 2024 · 0 comments
Closed

EraseFlash fails if address exceeds 0xFFFF #5

bessman opened this issue Dec 23, 2024 · 0 comments
Assignees
Labels

Comments

@bessman
Copy link
Collaborator

bessman commented Dec 23, 2024

EraseFlash returns BAD_ADDRESS when given an address over 0xFFFF. This is due to this check 1:

// check to make sure page is aligned here.
if ( (eraseAddress & FLASH_ERASE_PAGE_MASK) != eraseAddress)
{
    goodErase = false;
}

FLASH_ERASE_PAGE_MASK is 2:

#define FLASH_ERASE_PAGE_MASK  (~((FLASH_ERASE_PAGE_SIZE_IN_INSTRUCTIONS*2) - 1))

and FLASH_ERASE_PAGE_SIZE_IN_INSTRUCTIONS is 3:

#define FLASH_ERASE_PAGE_SIZE_IN_INSTRUCTIONS 1024U

1024U is an unsigned int, which is 16-bits wide in the PIC24. Therefore, when eraseAddress is greater than 0xFFFF (the largest value which fits in an unsigned int), the check incorrectly concludes that the address is misaligned.

Defining FLASH_ERASE_PAGE_SIZE_IN_INSTRUCTIONS as 1024UL should fix this.

In practice, the impact of this bug is low; if the entire program area is erased at once, the bug does not trigger. I only noticed it when I tried to erase one page at a time.

Footnotes

  1. https://github.com/fossasia/pslab-bootloader/blob/5e1616dc1366fb7e08c73000c62aba77432c1327/src/mcc_generated_files/boot/boot_process.c#L282

  2. https://github.com/fossasia/pslab-bootloader/blob/5e1616dc1366fb7e08c73000c62aba77432c1327/src/mcc_generated_files/boot/boot_process.c#L69

  3. https://github.com/fossasia/pslab-bootloader/blob/5e1616dc1366fb7e08c73000c62aba77432c1327/src/mcc_generated_files/memory/flash.h#L31

@bessman bessman added the bug label Dec 23, 2024
@bessman bessman self-assigned this Dec 23, 2024
bessman added a commit to bessman/pslab-bootloader that referenced this issue Dec 23, 2024
bessman added a commit to bessman/pslab-bootloader that referenced this issue Dec 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant