Skip to content

Commit

Permalink
Old technology of baseband extracting for fix some errors with future…
Browse files Browse the repository at this point in the history
…restore
  • Loading branch information
s0uthwest committed Jan 3, 2020
1 parent 915c3d1 commit c97e02e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 175 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ case ${host_os} in
esac
AM_CONDITIONAL(WIN32, test x$win32 = xtrue)

AC_CHECK_FUNCS([strsep strcspn mkstemp realpath])
AC_CHECK_FUNCS([strsep strcspn realpath])
if test x$ac_cv_func_strsep != xyes; then
if test x$ac_cv_func_strcspn != xyes; then
AC_MSG_ERROR([You need either strsep or strcspn to build $PACKAGE])
Expand Down
162 changes: 0 additions & 162 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,168 +299,6 @@ int mkdir_with_parents(const char *dir, int mode)
return res;
}

#ifndef HAVE_MKSTEMP
/* Based on libc's __gen_tempname() from sysdeps/posix/tempname.c
Copyright (C) 1991-2018 Free Software Foundation, Inc.
With changes from https://stackoverflow.com/a/6036308 and some
additional changes. */
int mkstemp(char *tmpl)
{
static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
int len;
char *XXXXXX;
static unsigned long long value;
unsigned long long random_time_bits;
unsigned int count;
int fd = -1;
int save_errno = errno;

/* A lower bound on the number of temporary files to attempt to
generate. The maximum total number of temporary file names that
can exist for a given template is 62**6. It should never be
necessary to try all these combinations. Instead if a reasonable
number of names is tried (we define reasonable as 62**3) fail to
give the system administrator the chance to remove the problems. */
#define ATTEMPTS_MIN (62 * 62 * 62)

/* The number of times to attempt to generate a temporary file. To
conform to POSIX, this must be no smaller than TMP_MAX. */
#if ATTEMPTS_MIN < TMP_MAX
unsigned int attempts = TMP_MAX;
#else
unsigned int attempts = ATTEMPTS_MIN;
#endif

len = strlen (tmpl);
if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
{
errno = EINVAL;
return -1;
}

/* This is where the Xs start. */
XXXXXX = &tmpl[len - 6];

/* Get some more or less random data. */
#ifdef WIN32
{
SYSTEMTIME stNow;
FILETIME ftNow;

// get system time
GetSystemTime(&stNow);
if (!SystemTimeToFileTime(&stNow, &ftNow))
{
errno = -1;
return -1;
}

random_time_bits = (((unsigned long long)ftNow.dwHighDateTime << 32)
| (unsigned long long)ftNow.dwLowDateTime);
}
value += random_time_bits ^ ((unsigned long long)GetCurrentProcessId() << 32 | (unsigned long long)GetCurrentThreadId());
#else
{
struct timeval tvNow = {0, 0};
gettimeofday(&tvNow, NULL);
random_time_bits = (((unsigned long long)tvNow.tv_sec << 32)
| (unsigned long long)tvNow.tv_usec);
}
value += random_time_bits ^ ((unsigned long long)getpid() << 32 | (unsigned long long)(uintptr_t)pthread_self());
#endif

for (count = 0; count < attempts; value += 7777, ++count)
{
unsigned long long v = value;

/* Fill in the random bits. */
XXXXXX[0] = letters[v % 62];
v /= 62;
XXXXXX[1] = letters[v % 62];
v /= 62;
XXXXXX[2] = letters[v % 62];
v /= 62;
XXXXXX[3] = letters[v % 62];
v /= 62;
XXXXXX[4] = letters[v % 62];
v /= 62;
XXXXXX[5] = letters[v % 62];

#ifdef WIN32
fd = open (tmpl, O_RDWR | O_CREAT | O_EXCL, _S_IREAD | _S_IWRITE);
#else
fd = open (tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
#endif
if (fd >= 0)
{
errno = save_errno;
return fd;
}
else if (errno != EEXIST)
return -1;
}

/* We got out of the loop because we ran out of combinations to try. */
errno = EEXIST;
return -1;
}
#endif

char *get_temp_filename(const char *prefix)
{
char *result = NULL;
char *tmpdir;
size_t lt;
size_t lp;
const char *TMPVARS[] = { "TMPDIR", "TMP", "TEMP", "TEMPDIR", NULL };
int i = 0;
int fd;

/* check the prefix parameter */
if (!prefix) {
prefix = "tmp_";
}
#ifdef WIN32
if (strchr(prefix, '/') || strchr(prefix, '\\')) return NULL;
#else
if (strchr(prefix, '/')) return NULL;
#endif

while (TMPVARS[i] && ((tmpdir = getenv(TMPVARS[i])) == NULL)) i++;
if (!tmpdir || access(tmpdir, W_OK|X_OK) != 0) {
#ifdef WIN32
tmpdir = "C:\\WINDOWS\\TEMP";
#else
tmpdir = P_tmpdir;
#endif
}
if (!tmpdir || access(tmpdir, W_OK|X_OK) != 0) {
return NULL;
}

lt = strlen(tmpdir);
if (lt < 1) {
return NULL;
}
lp = strlen(prefix);
result = malloc(lt + lp + 8);
strncpy(result, tmpdir, lt);
#ifdef WIN32
if (tmpdir[lt-1] != '/' && tmpdir[lt-1] != '\\') result[lt++] = '\\';
#else
if (tmpdir[lt-1] != '/') result[lt++] = '/';
#endif
strncpy(result + lt, prefix, lp);
strcpy(result + lt + lp, "XXXXXX");
fd = mkstemp(result);
if (fd < 0) {
free(result);
result = NULL;
}
close(fd);
return result;
}

void idevicerestore_progress(struct idevicerestore_client_t* client, int step, double progress)
{
if(client && client->progress_cb) {
Expand Down
2 changes: 0 additions & 2 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,6 @@ char *generate_guid(void);

int mkdir_with_parents(const char *dir, int mode);

char *get_temp_filename(const char *prefix);

void idevicerestore_progress(struct idevicerestore_client_t* client, int step, double progress);

#ifndef HAVE_STRSEP
Expand Down
2 changes: 1 addition & 1 deletion src/idevicerestore.c
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@ int idevicerestore_start(struct idevicerestore_client_t* client)
unlock_file(&li);
if (!extf) {
// use temp filename
filesystem = get_temp_filename("ipsw_");
filesystem = tempnam(NULL, "ipsw_");
if (!filesystem) {
error("WARNING: Could not get temporary filename, using '%s' in current directory\n", fsname);
filesystem = strdup(fsname);
Expand Down
28 changes: 19 additions & 9 deletions src/restore.c
Original file line number Diff line number Diff line change
Expand Up @@ -1668,19 +1668,29 @@ int restore_send_baseband_data(restored_client_t restore, struct idevicerestore_
}

// extract baseband firmware to temp file
bbfwtmp = get_temp_filename("bbfw_");
bbfwtmp = tempnam(NULL, client->udid);
if (!bbfwtmp) {
size_t l = strlen(client->udid);
bbfwtmp = malloc(l + 10);
strcpy(bbfwtmp, "bbfw_");
strncpy(bbfwtmp + 5, client->udid, l);
strcpy(bbfwtmp + 5 + l, ".tmp");
error("WARNING: Could not generate temporary filename, using %s in current directory\n", bbfwtmp);
error("WARNING: Could not generate temporary filename, using bbfw.tmp\n");
bbfwtmp = strdup("bbfw.tmp");
}
if (ipsw_extract_to_file(client->ipsw, bbfwpath, bbfwtmp) != 0) {
error("ERROR: Unable to extract baseband firmware from ipsw\n");
goto leave;
}
plist_free(response);
return -1;
}else{
FILE *f1 = fopen(client->bbfwtmp,"rb");
FILE *f2 = fopen(bbfwtmp,"wb");
size_t s;
fseek(f1, 0, SEEK_END);
s = ftell(f1);
fseek(f1, 0, SEEK_SET);

char * buf = malloc(s);
fread(buf, 1, s, f1);
fwrite(buf, 1, s, f2);
fclose(f1);
fclose(f2);
}

if (bb_nonce && !client->restore->bbtss) {
// keep the response for later requests
Expand Down

0 comments on commit c97e02e

Please sign in to comment.