void mgos_upd_hal_ctx_free(struct mgos_upd_hal_ctx *ctx) { if (ctx == NULL) return; if (ctx->cur_fh >= 0) sl_FsClose(ctx->cur_fh, NULL, NULL, 0); if (ctx->cur_fn != NULL) sl_FsDel(ctx->cur_fn, 0); memset(ctx, 0, sizeof(*ctx)); free(ctx); }
static int prepare_to_write(struct mgos_upd_hal_ctx *ctx, const struct mgos_upd_file_info *fi, const char *fname, uint32_t falloc, struct json_token *part) { struct json_token expected_sha1 = JSON_INVALID_TOKEN; json_scanf(part->ptr, part->len, "{cs_sha1: %T}", &expected_sha1); if (verify_checksum(fname, fi->size, &expected_sha1)) { LOG(LL_INFO, ("Digest matched for %s %u (%.*s)", fname, (unsigned int) fi->size, (int) expected_sha1.len, expected_sha1.ptr)); return 0; } LOG(LL_INFO, ("Storing %s %u -> %s %u (%.*s)", fi->name, (unsigned int) fi->size, fname, (unsigned int) falloc, (int) expected_sha1.len, expected_sha1.ptr)); ctx->cur_fn = (const _u8 *) fname; sl_FsDel(ctx->cur_fn, 0); _i32 r = sl_FsOpen(ctx->cur_fn, FS_MODE_OPEN_CREATE(falloc, 0), NULL, &ctx->cur_fh); if (r < 0) { ctx->status_msg = "Failed to create file"; return r; } return 1; }
int fs_slfs_unlink(const char *pathname) { /* * Apply path manipulations again, in case we got here directly * (via TI libc's "add_device"). */ pathname = drop_dir(pathname, NULL); return set_errno(sl_fs_to_errno(sl_FsDel((const _u8 *) pathname, 0))); }
void resetConfig() { sl_FsDel((unsigned char *)CONFIG_FILENAME, 0); memset(g_ApplicationID, 0, sizeof(g_ApplicationID)); memset(g_ClientKey, 0, sizeof(g_ClientKey)); memset(g_InstallationID, 0, sizeof(g_InstallationID)); memset(g_SessionToken, 0, sizeof(g_SessionToken)); memset(g_DeviceName, 0, sizeof(g_DeviceName)); }
//***************************************************************************** //! Load the proper image based on the information from the boot info //! and launch it. //***************************************************************************** static void bootmgr_image_loader(sBootInfo_t *psBootInfo) { _i32 fhandle; _u8 *image; // search for the active image switch (psBootInfo->ActiveImg) { case IMG_ACT_UPDATE1: image = (unsigned char *)IMG_UPDATE1; break; case IMG_ACT_UPDATE2: image = (unsigned char *)IMG_UPDATE2; break; default: image = (unsigned char *)IMG_FACTORY; break; } // do we have a new image that needs to be verified? if ((psBootInfo->ActiveImg != IMG_ACT_FACTORY) && (psBootInfo->Status == IMG_STATUS_CHECK)) { if (!bootmgr_verify(image)) { // verification failed, delete the broken file sl_FsDel(image, 0); // switch to the previous image psBootInfo->ActiveImg = psBootInfo->PrevImg; psBootInfo->PrevImg = IMG_ACT_FACTORY; } // in any case, change the status to "READY" psBootInfo->Status = IMG_STATUS_READY; // write the new boot info if (!sl_FsOpen((unsigned char *)IMG_BOOT_INFO, FS_MODE_OPEN_WRITE, NULL, &fhandle)) { sl_FsWrite(fhandle, 0, (unsigned char *)psBootInfo, sizeof(sBootInfo_t)); // close the file sl_FsClose(fhandle, 0, 0, 0); } } // this one might modify the boot info hence it MUST be called after // bootmgr_verify! (so that the changes are not saved to flash) wait_for_safe_boot(psBootInfo); // select the active image again, since it might have changed switch (psBootInfo->ActiveImg) { case IMG_ACT_UPDATE1: image = (unsigned char *)IMG_UPDATE1; break; case IMG_ACT_UPDATE2: image = (unsigned char *)IMG_UPDATE2; break; default: image = (unsigned char *)IMG_FACTORY; break; } bootmgr_load_and_execute(image); }
static _i32 fs_create_container(int cidx, _u32 fs_size) { _i32 fh = -1; const _u8 *fname = container_fname(cidx); _u32 fsize = FS_CONTAINER_SIZE(fs_size); int r = sl_FsDel(fname, 0); dprintf(("del %s -> %d\n", fname, r)); r = sl_FsOpen(fname, FS_MODE_OPEN_CREATE(fsize, 0), NULL, &fh); dprintf(("open %s %d -> %d %d\n", fname, (int) fsize, (int) r, (int) fh)); if (r != 0) return r; return fh; }
int32_t SLFS::del(const uint8_t *filename) { if (filehandle) { retval = SLFS_LIB_ERR_FILE_ALREADY_OPEN; return retval; // In case the file we have open is the one the user is trying to delete... } retval = sl_FsDel((unsigned char*)filename, 0); if (retval != SL_FS_OK) return retval; return retval; }
_i32 fs_delete_container(const char *cpfx, int cidx) { _i32 ret = -1; SlFsFileInfo_t fi; _u8 fname[MAX_FS_CONTAINER_FNAME_LEN]; fs_container_fname(cpfx, cidx, fname); ret = sl_FsGetInfo(fname, 0, &fi); if (ret == 0) { LOG(LL_INFO, ("Deleting %s", fname)); ret = sl_FsDel(fname, 0); } return ret; }
_i32 fs_create_container(const char *cpfx, int cidx, _u32 fs_size) { _i32 fh = -1; _u8 fname[MAX_FS_CONTAINER_FNAME_LEN]; _u32 fsize = FS_CONTAINER_SIZE(fs_size); fs_container_fname(cpfx, cidx, fname); int r = sl_FsDel(fname, 0); DBG(("del %s -> %d", fname, r)); r = sl_FsOpen(fname, FS_MODE_OPEN_CREATE(fsize, 0), NULL, &fh); LOG((r == 0 ? LL_DEBUG : LL_ERROR), ("open %s %d -> %d %d", fname, (int) fsize, (int) r, (int) fh)); if (r != 0) return r; return fh; }
//***************************************************************************** //! Load the proper image based on information from boot info and executes it. //***************************************************************************** static void bootmgr_image_loader(sBootInfo_t *psBootInfo) { _i32 fhandle; if (safe_mode_boot()) { _u32 count = 0; while ((BOOTMGR_SAFE_MODE_ENTER_TOOGLE_MS * count++) < BOOTMGR_SAFE_MODE_ENTER_MS) { // toogle the led MAP_GPIOPinWrite(MICROPY_SYS_LED_PORT, MICROPY_SYS_LED_PORT_PIN, ~MAP_GPIOPinRead(MICROPY_SYS_LED_PORT, MICROPY_SYS_LED_PORT_PIN)); UtilsDelay(UTILS_DELAY_US_TO_COUNT(BOOTMGR_SAFE_MODE_ENTER_TOOGLE_MS * 1000)); } psBootInfo->ActiveImg = IMG_ACT_FACTORY; // turn the led off MAP_GPIOPinWrite(MICROPY_SYS_LED_PORT, MICROPY_SYS_LED_PORT_PIN, 0); // request a safe boot to the application PRCMRequestSafeBoot(); } // do we have a new update image that needs to be verified? else if ((psBootInfo->ActiveImg == IMG_ACT_UPDATE) && (psBootInfo->Status == IMG_STATUS_CHECK)) { if (!bootmgr_verify()) { // delete the corrupted file sl_FsDel((_u8 *)IMG_UPDATE, 0); // switch to the factory image psBootInfo->ActiveImg = IMG_ACT_FACTORY; } // in any case, set the status as "READY" psBootInfo->Status = IMG_STATUS_READY; // write the new boot info if (!sl_FsOpen((unsigned char *)IMG_BOOT_INFO, FS_MODE_OPEN_WRITE, NULL, &fhandle)) { sl_FsWrite(fhandle, 0, (unsigned char *)psBootInfo, sizeof(sBootInfo_t)); // close the file sl_FsClose(fhandle, 0, 0, 0); } } // now boot the active image if (IMG_ACT_UPDATE == psBootInfo->ActiveImg) { bootmgr_load_and_execute((unsigned char *)IMG_UPDATE); } else { bootmgr_load_and_execute((unsigned char *)IMG_FACTORY); } }
int fs_slfs_unlink(const char *filename) { return set_errno(sl_fs_to_errno(sl_FsDel((const _u8 *) filename, 0))); }
short saveClientState(ParseClientInternal *parseClient) { #ifdef CLIENT_DEBUG DEBUG_PRINT("[Parse] Saving client state\r\n"); #endif /* CLIENT_DEBUG */ snprintf(clientStateFileName, CLIENT_STATE_FILENAME_LEN, CLIENT_STATE_FILENAME, parseClient->applicationId); long clientStateFile = -1; sl_FsDel((unsigned char *)clientStateFileName, 0); short status = sl_FsOpen((unsigned char *)clientStateFileName, FS_MODE_OPEN_CREATE(65536, _FS_FILE_OPEN_FLAG_COMMIT | _FS_FILE_PUBLIC_WRITE), NULL, &clientStateFile); if (status < 0) { #ifdef CLIENT_DEBUG if (status == SL_FS_ERR_NO_AVAILABLE_BLOCKS) { DEBUG_PRINT("[Parse] No available blocks. Need to flash your device file system...\r\n"); } #endif /* CLIENT_DEBUG */ return status; } sl_FsClose(clientStateFile, 0, 0, 0); clientStateFile = -1; status = sl_FsOpen((unsigned char *)clientStateFileName, FS_MODE_OPEN_WRITE, NULL, &clientStateFile); if (status < 0) { #ifdef CLIENT_DEBUG if (status == SL_FS_ERR_NO_AVAILABLE_BLOCKS) { DEBUG_PRINT("[Parse] No available blocks. Need to flash your device file system...\r\n"); } #endif /* CLIENT_DEBUG */ sl_FsClose(clientStateFile, 0, 0, 0); return status; } #ifdef CLIENT_DEBUG DEBUG_PRINT("[Parse] Installation Id: %s\r\n", parseClient->installationId); #endif /* CLIENT_DEBUG */ long writeOffset = 0; status = sl_FsWrite(clientStateFile, writeOffset, (unsigned char *)parseClient->installationId, sizeof(parseClient->installationId)); if (status < 0) { #ifdef CLIENT_DEBUG if (status == SL_FS_ERR_NO_AVAILABLE_BLOCKS) { DEBUG_PRINT("[Parse] No available blocks. Need to flash your device file system...\r\n"); } #endif /* CLIENT_DEBUG */ sl_FsClose(clientStateFile, 0, 0, 0); return status; } #ifdef CLIENT_DEBUG DEBUG_PRINT("[Parse] Session token: %s\r\n", parseClient->sessionToken); #endif /* CLIENT_DEBUG */ writeOffset = writeOffset + status; status = sl_FsWrite(clientStateFile, writeOffset, (unsigned char *)parseClient->sessionToken, sizeof(parseClient->sessionToken)); if (status < 0) { #ifdef CLIENT_DEBUG if (status == SL_FS_ERR_NO_AVAILABLE_BLOCKS) { DEBUG_PRINT("[Parse] No available blocks. Need to flash your device file system...\r\n"); } #endif /* CLIENT_DEBUG */ sl_FsClose(clientStateFile, 0, 0, 0); return status; } #ifdef CLIENT_DEBUG DEBUG_PRINT("[Parse] Last push hash: %s\r\n", parseClient->lastPushHash); #endif /* CLIENT_DEBUG */ writeOffset = writeOffset + status; status = sl_FsWrite(clientStateFile, writeOffset, (unsigned char *)parseClient->lastPushHash, sizeof(parseClient->lastPushHash)); if (status < 0) { #ifdef CLIENT_DEBUG if (status == SL_FS_ERR_NO_AVAILABLE_BLOCKS) { DEBUG_PRINT("[Parse] No available blocks. Need to flash your device file system...\r\n"); } #endif /* CLIENT_DEBUG */ sl_FsClose(clientStateFile, 0, 0, 0); return status; } sl_FsClose(clientStateFile, 0, 0, 0); return status; }
int GetFileFromServer(const int socket, const char* filename, const char* filenameInFlash) { int transfer_len = 0; long retVal = 0; long fileHandle = -1; unsigned long Token = 0, bytesReceived = 0; unsigned char *pBuff = 0; unsigned long file_size = 0; unsigned char buffer[MAX_BUFF_SIZE + 1]; UART_PRINT("Start downloading the file %s\r\n", filename); memset(buffer, 0, sizeof(buffer)); // Puts together the HTTP GET string. strcpy((char *) buffer, PREFIX_BUFFER); strcat((char *) buffer, filename); strcat((char *) buffer, POST_BUFFER); // Send the HTTP GET string to the opened TCP/IP socket. transfer_len = sl_Send(socket, buffer, strlen((const char *) buffer), 0); if (transfer_len < 0) { // error UART_PRINT("Socket Send Error\r\n"); return -1; } memset(buffer, 0, sizeof(buffer)); // get the reply from the server in buffer. transfer_len = sl_Recv(socket, buffer, MAX_BUFF_SIZE, 0); if (transfer_len > 0) { // Check for 404 return code if (strstr((const char *) buffer, HTTP_FILE_NOT_FOUND) != 0) { UART_PRINT("File not found, check the file and try again\r\n"); return -1; } // if not "200 OK" return error if (strstr((const char *) buffer, HTTP_STATUS_OK) == 0) { UART_PRINT("Error during downloading the file\r\n"); return -1; } // check if content length is transfered with headers pBuff = (unsigned char *) strstr((const char *) buffer, HTTP_CONTENT_LENGTH); if (pBuff != 0) { pBuff += strlen(HTTP_CONTENT_LENGTH); while (*pBuff != 0x0d) pBuff++; unsigned long multiplicant = 1; pBuff--; while (*pBuff != ' ') { file_size += (*pBuff - '0') * multiplicant; multiplicant *= 10; pBuff--; } if (file_size == 0) { UART_PRINT("\nContent Length not detected.\r\n"); return -1; } UART_PRINT("Content Length %d\r\n", file_size); } else { UART_PRINT("\nContent Length not detected.\r\n"); return -1; } // "\r\n\r\n" marks the end of headers pBuff = (unsigned char *) strstr((const char *) buffer, HTTP_END_OF_HEADER); if (pBuff == 0) { UART_PRINT("Invalid response\r\n"); return -1; } // Increment by 4 to skip "\r\n\r\n" pBuff += 4; // Adjust buffer data length for header size transfer_len -= (pBuff - buffer); } // Delete eventually existing temporary file sl_FsDel((unsigned char *) filenameInFlash, Token); // Create a temporary file to save the downloaded file // open a user file for writing retVal = sl_FsOpen((unsigned char *) filenameInFlash, FS_MODE_OPEN_WRITE, &Token, &fileHandle); if (retVal < 0) { // File Doesn't exit create a new of file_size KB file retVal = sl_FsOpen((unsigned char *) filenameInFlash, FS_MODE_OPEN_CREATE(file_size, _FS_FILE_PUBLIC_WRITE), &Token, &fileHandle); if (retVal < 0) { UART_PRINT("Error during opening the temporary file\r\n"); return -1; } } while (bytesReceived < file_size) { retVal = sl_FsWrite(fileHandle, bytesReceived, (unsigned char *) pBuff, transfer_len); if (retVal < 0) { retVal = sl_FsClose(fileHandle, 0, 0, 0); retVal = sl_FsDel((unsigned char *) filenameInFlash, Token); UART_PRINT("Error during writing the file\r\n"); return -1; } bytesReceived += transfer_len; if (bytesReceived == file_size) break; memset(buffer, 0, sizeof(buffer)); transfer_len = sl_Recv(socket, buffer, MAX_BUFF_SIZE, 0); pBuff = buffer; } // Close the opened file retVal = sl_FsClose(fileHandle, 0, 0, 0); if (bytesReceived != file_size) { UART_PRINT("Error While File Download\r\n"); retVal = sl_FsDel((unsigned char *) filenameInFlash, Token); return -1; } else { UART_PRINT("\nDownloading File Completed\r\n"); } return 0; }