/** * @brief Return the device size by using the sysfs * @param sysfspath * @return */ static off_t mmc_device_size_sysfs(const char *sysfspath) { char sizestr[16]; int rc = readsysfs(sysfspath, sizestr, sizeof(sizestr)); if (rc <= 0) return 0; return strtoll(sizestr, NULL, 0) * FWUP_BLOCK_SIZE; }
int mmc_is_path_at_device_offset(const char *file_path, off_t block_offset) { struct stat file_st; if (stat(file_path, &file_st) < 0) return -1; // Now check that the offset is in the right place by reading the sysfs file. char start_path[64]; snprintf(start_path, sizeof(start_path), "/sys/dev/block/%d:%d/start", major(file_st.st_dev), minor(file_st.st_dev)); char start[16]; if (readsysfs(start_path, start, sizeof(start)) <= 0) return -1; off_t start_offset = strtoull(start, NULL, 0); return start_offset == block_offset ? 1 : 0; }
int setsysfs(char *dir, char *entry, char *value) { char filename[PATH_MAX]; char *oldvalue; int fd, ret = 0; if (dir == NULL) snprintf(filename, PATH_MAX, "%s/%s", MICSYSFSDIR, entry); else snprintf(filename, PATH_MAX, "%s/%s/%s", MICSYSFSDIR, dir, entry); oldvalue = readsysfs(dir, entry); fd = open(filename, O_RDWR); if (fd < 0) { ret = errno; mpsslog("Failed to open sysfs entry '%s': %s\n", filename, strerror(errno)); goto done; } if (!oldvalue || strcmp(value, oldvalue)) { if (write(fd, value, strlen(value)) < 0) { ret = errno; mpsslog("Failed to write new sysfs entry '%s': %s\n", filename, strerror(errno)); } } close(fd); done: if (oldvalue) free(oldvalue); return ret; }