-
Notifications
You must be signed in to change notification settings - Fork 14
storage: port host-flash device to the Phoenix-RTOS platform
#574
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |||||||||||||||||
| #include <stdio.h> | ||||||||||||||||||
| #include <unistd.h> | ||||||||||||||||||
| #include <string.h> | ||||||||||||||||||
| #include <assert.h> | ||||||||||||||||||
|
|
||||||||||||||||||
| #include "host-flash.h" | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -33,76 +34,96 @@ static struct { | |||||||||||||||||
| } hostflash_common; | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| ssize_t hostflash_read(struct _meterfs_devCtx_t *devCtx, off_t offs, void *buff, size_t bufflen) | ||||||||||||||||||
| static ssize_t hostflash_safeRead(void *buff, size_t bufflen, off_t offset) | ||||||||||||||||||
| { | ||||||||||||||||||
| ssize_t stat; | ||||||||||||||||||
| int ret = lseek(hostflash_common.filefd, offset, SEEK_SET); | ||||||||||||||||||
| if (ret < 0) { | ||||||||||||||||||
| return (ssize_t)ret; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| int readsz = 0; | ||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||||||||||
| ssize_t len; | ||||||||||||||||||
| do { | ||||||||||||||||||
| len = read(hostflash_common.filefd, (char *)buff + readsz, bufflen - readsz); | ||||||||||||||||||
| if (len < 0) { | ||||||||||||||||||
| if (errno == EINTR) { | ||||||||||||||||||
| continue; | ||||||||||||||||||
| } | ||||||||||||||||||
| return len; | ||||||||||||||||||
| } | ||||||||||||||||||
| readsz += len; | ||||||||||||||||||
| } while ((len > 0) && (readsz != bufflen)); | ||||||||||||||||||
|
|
||||||||||||||||||
| (void)devCtx; | ||||||||||||||||||
| return readsz; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| if ((devCtx == NULL) || (devCtx->state == 0) || ((offs + bufflen) > hostflash_common.flashsz)) { | ||||||||||||||||||
| return -EINVAL; | ||||||||||||||||||
|
|
||||||||||||||||||
| static ssize_t hostflash_safeWrite(const void *buff, size_t bufflen, off_t offset) | ||||||||||||||||||
| { | ||||||||||||||||||
| int ret = lseek(hostflash_common.filefd, offset, SEEK_SET); | ||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The return type of
Suggested change
|
||||||||||||||||||
| if (ret < 0) { | ||||||||||||||||||
| return (ssize_t)ret; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| while ((stat = pread(hostflash_common.filefd, (void *)((char *)buff + readsz), bufflen - readsz, offs + readsz)) != 0) { | ||||||||||||||||||
| if (stat < 0) { | ||||||||||||||||||
| int writesz = 0; | ||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||||||||||
| ssize_t len; | ||||||||||||||||||
| do { | ||||||||||||||||||
| len = write(hostflash_common.filefd, (char *)buff + writesz, bufflen - writesz); | ||||||||||||||||||
| if (len < 0) { | ||||||||||||||||||
| if (errno == EINTR) { | ||||||||||||||||||
| continue; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| return stat; | ||||||||||||||||||
| return len; | ||||||||||||||||||
| } | ||||||||||||||||||
| writesz += len; | ||||||||||||||||||
| } while ((len > 0) && (writesz != bufflen)); | ||||||||||||||||||
|
|
||||||||||||||||||
| readsz += stat; | ||||||||||||||||||
| if (readsz == bufflen) { | ||||||||||||||||||
| break; | ||||||||||||||||||
| } | ||||||||||||||||||
| return writesz; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| ssize_t hostflash_read(struct _meterfs_devCtx_t *devCtx, off_t offs, void *buff, size_t bufflen) | ||||||||||||||||||
| { | ||||||||||||||||||
| if ((devCtx == NULL) || (devCtx->state == 0) || ((offs + bufflen) > hostflash_common.flashsz)) { | ||||||||||||||||||
| return -EINVAL; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| return readsz; | ||||||||||||||||||
| return hostflash_safeRead(buff, bufflen, offs); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| ssize_t hostflash_write(struct _meterfs_devCtx_t *devCtx, off_t offs, const void *buff, size_t bufflen) | ||||||||||||||||||
| { | ||||||||||||||||||
| char tempTab[256]; | ||||||||||||||||||
| size_t wrote = 0, i; | ||||||||||||||||||
| int len; | ||||||||||||||||||
| ssize_t stat, readsz; | ||||||||||||||||||
| size_t wrote = 0; | ||||||||||||||||||
|
|
||||||||||||||||||
| if ((devCtx == NULL) || (devCtx->state == 0) || ((offs + bufflen) > hostflash_common.flashsz)) { | ||||||||||||||||||
| return -EINVAL; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| while (wrote != bufflen) { | ||||||||||||||||||
| size_t len; | ||||||||||||||||||
| if ((bufflen - wrote) >= sizeof(tempTab)) { | ||||||||||||||||||
| len = sizeof(tempTab); | ||||||||||||||||||
| } | ||||||||||||||||||
| else { | ||||||||||||||||||
| len = bufflen - wrote; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| readsz = hostflash_read(devCtx, offs + wrote, tempTab, len); | ||||||||||||||||||
| ssize_t readsz = hostflash_safeRead(tempTab, len, offs + wrote); | ||||||||||||||||||
| if (readsz <= 0) { | ||||||||||||||||||
| return readsz; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| for (i = 0; i < readsz; i++) { | ||||||||||||||||||
| tempTab[i] = tempTab[i] & *((const char *)buff + wrote + i); | ||||||||||||||||||
| for (ssize_t i = 0; i < readsz; i++) { | ||||||||||||||||||
| tempTab[i] &= *((const char *)buff + wrote + i); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| len = 0; | ||||||||||||||||||
| while (len != readsz) { | ||||||||||||||||||
| stat = pwrite(hostflash_common.filefd, &tempTab, readsz - len, offs + wrote + len); | ||||||||||||||||||
| if (stat < 0) { | ||||||||||||||||||
| if (errno == EINTR) | ||||||||||||||||||
| continue; | ||||||||||||||||||
|
|
||||||||||||||||||
| return stat; | ||||||||||||||||||
| } | ||||||||||||||||||
| len += stat; | ||||||||||||||||||
| ssize_t stat = hostflash_safeWrite(tempTab, readsz, offs + wrote); | ||||||||||||||||||
| if (stat < 0) { | ||||||||||||||||||
| return stat; | ||||||||||||||||||
| } | ||||||||||||||||||
| wrote += len; | ||||||||||||||||||
| wrote += stat; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| return (ssize_t)wrote; | ||||||||||||||||||
|
|
@@ -112,35 +133,22 @@ ssize_t hostflash_write(struct _meterfs_devCtx_t *devCtx, off_t offs, const void | |||||||||||||||||
| int hostflash_sectorErase(struct _meterfs_devCtx_t *devCtx, off_t offs) | ||||||||||||||||||
| { | ||||||||||||||||||
| char tempTab[256]; | ||||||||||||||||||
| ssize_t len = sizeof(tempTab); | ||||||||||||||||||
| size_t erased = 0; | ||||||||||||||||||
| unsigned int sectorAddr; | ||||||||||||||||||
| int stat; | ||||||||||||||||||
|
|
||||||||||||||||||
| if ((devCtx == NULL) || (devCtx->state == 0) || (offs >= hostflash_common.flashsz)) { | ||||||||||||||||||
| return -EINVAL; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| (void)memset(tempTab, 0xff, sizeof(tempTab)); | ||||||||||||||||||
|
|
||||||||||||||||||
| sectorAddr = (offs / hostflash_common.sectorsz) * hostflash_common.sectorsz; | ||||||||||||||||||
| off_t sectorAddr = (offs / hostflash_common.sectorsz) * hostflash_common.sectorsz; | ||||||||||||||||||
|
|
||||||||||||||||||
| while (erased != hostflash_common.sectorsz) { | ||||||||||||||||||
| stat = pwrite(hostflash_common.filefd, tempTab, len, sectorAddr + erased); | ||||||||||||||||||
| if (stat < 0) { | ||||||||||||||||||
| if (errno == EINTR) { | ||||||||||||||||||
| continue; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| return stat; | ||||||||||||||||||
| } | ||||||||||||||||||
| assert(hostflash_common.sectorsz % sizeof(tempTab) == 0); | ||||||||||||||||||
|
|
||||||||||||||||||
| len -= stat; | ||||||||||||||||||
| if (len == 0) { | ||||||||||||||||||
| len = sizeof(tempTab); | ||||||||||||||||||
| for (size_t erased = 0; erased < hostflash_common.sectorsz; erased += sizeof(tempTab)) { | ||||||||||||||||||
| ssize_t stat = hostflash_safeWrite(tempTab, sizeof(tempTab), sectorAddr + erased); | ||||||||||||||||||
| if (stat < 0) { | ||||||||||||||||||
| return (int)stat; | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+148
to
151
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This implementation does not handle short writes from
Suggested change
|
||||||||||||||||||
|
|
||||||||||||||||||
| erased += stat; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| return 0; | ||||||||||||||||||
|
|
||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return type of
lseekisoff_t, but it's being stored in anint. This could lead to truncation on 64-bit systems whereoff_tis larger thanint. Please useoff_tfor theretvariable to ensure correctness across different architectures.