freemyipod r897 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r896‎ | r897 | r898 >
Date:14:09, 16 August 2013
Author:user890104
Status:new
Tags:
Comment:
iTunesDFU for Nano 4G
Modified paths:
  • /tools/itunesdfu/bootstrap_ipodnano4g_itunes (added) (history)
  • /tools/itunesdfu/bootstrap_ipodnano4g_itunes/Makefile (modified) (history)
  • /tools/itunesdfu/bootstrap_ipodnano4g_itunes/main.c (modified) (history)

Diff [purge]

Index: tools/itunesdfu/bootstrap_ipodnano4g_itunes/Makefile
@@ -0,0 +1,85 @@
 2+NAME := bootstrap_ipodnano4g_itunes
 3+DFUIMAGE ?= ../../../apps/installer-ipodnano4g/build/bootstrap-ipodnano4g.dfu
 4+
 5+ifeq ($(shell uname),WindowsNT)
 6+CCACHE :=
 7+else
 8+CCACHE := $(shell which ccache)
 9+endif
 10+
 11+CC := $(CCACHE) gcc
 12+LD := $(CCACHE) gcc
 13+
 14+CFLAGS += -Os -fomit-frame-pointer "-DDFUIMAGE=\"$(DFUIMAGE)\""
 15+LDFLAGS += -Wl,-s
 16+
 17+preprocess = $(shell $(CC) $(PPCFLAGS) $(2) -E -P -x c $(1) | grep -v "^\#")
 18+preprocesspaths = $(shell $(CC) $(PPCFLAGS) $(2) -E -P -x c $(1) | grep -v "^\#" | sed -e "s:^..*:$(dir $(1))&:" | sed -e "s:^\\./::")
 19+
 20+REVISION := $(shell svnversion .)
 21+REVISIONINT := $(shell echo $(REVISION) | sed -e "s/[^0-9].*$$//")
 22+
 23+SRC := $(call preprocesspaths,SOURCES,-I. -I..)
 24+OBJ := $(SRC:%.c=build/%.o)
 25+OBJ := $(OBJ:%.S=build/%.o)
 26+
 27+all: $(NAME)
 28+
 29+-include $(OBJ:%=%.dep)
 30+
 31+$(NAME): build/$(NAME).exe
 32+
 33+build/resources.o: $(DFUIMAGE)
 34+
 35+build/$(NAME).exe: $(OBJ)
 36+ @echo [LD] $@
 37+ @$(LD) -o $@ $(OBJ) $(LDFLAGS)
 38+
 39+build/%.o: %.c build/version.h
 40+ @echo [CC] $<
 41+ifeq ($(shell uname),WindowsNT)
 42+ @-if not exist $(subst /,\,$(dir $@)) md $(subst /,\,$(dir $@))
 43+else
 44+ @-mkdir -p $(dir $@)
 45+endif
 46+ @$(CC) -c $(CFLAGS) -o $@ $<
 47+ @$(CC) -MM $(CFLAGS) $< > $@.dep.tmp
 48+ @sed -e "s|.*:|$@:|" < $@.dep.tmp > $@.dep
 49+ifeq ($(shell uname),WindowsNT)
 50+ @sed -e "s/.*://" -e "s/\\$$//" < $@.dep.tmp | fmt -1 | sed -e "s/^ *//" -e "s/$$/:/" >> $@.dep
 51+else
 52+ @sed -e 's/.*://' -e 's/\\$$//' < $@.dep.tmp | fmt -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $@.dep
 53+endif
 54+ @rm -f $@.dep.tmp
 55+
 56+build/%.o: %.S build/version.h
 57+ @echo [CC] $<
 58+ifeq ($(shell uname),WindowsNT)
 59+ @-if not exist $(subst /,\,$(dir $@)) md $(subst /,\,$(dir $@))
 60+else
 61+ @-mkdir -p $(dir $@)
 62+endif
 63+ @$(CC) -c $(CFLAGS) -o $@ $<
 64+ @$(CC) -MM $(CFLAGS) $< > $@.dep.tmp
 65+ @sed -e "s|.*:|$@:|" < $@.dep.tmp > $@.dep
 66+ifeq ($(shell uname),WindowsNT)
 67+ @sed -e "s/.*://" -e "s/\\$$//" < $@.dep.tmp | fmt -1 | sed -e "s/^ *//" -e "s/$$/:/" >> $@.dep
 68+else
 69+ @sed -e 's/.*://' -e 's/\\$$//' < $@.dep.tmp | fmt -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $@.dep
 70+endif
 71+ @rm -f $@.dep.tmp
 72+
 73+build/version.h: version.h ../../../.svn/entries
 74+ @echo [PP] $<
 75+ifeq ($(shell uname),WindowsNT)
 76+ @-if not exist build md build
 77+ @sed -e "s/\$$REVISION\$$/$(REVISION)/" -e "s/\$$REVISIONINT\$$/$(REVISIONINT)/" < $< > $@
 78+else
 79+ @-mkdir -p build
 80+ @sed -e 's/\$$REVISION\$$/$(REVISION)/' -e 's/\$$REVISIONINT\$$/$(REVISIONINT)/' < $< > $@
 81+endif
 82+
 83+clean:
 84+ @rm -rf build
 85+
 86+.PHONY: all clean $(NAME)
Index: tools/itunesdfu/bootstrap_ipodnano4g_itunes/main.c
@@ -0,0 +1,155 @@
 2+//
 3+//
 4+// Copyright 2011 TheSeven, user890104
 5+//
 6+//
 7+// This file is part of emCORE.
 8+//
 9+// emCORE is free software: you can redistribute it and/or
 10+// modify it under the terms of the GNU General Public License as
 11+// published by the Free Software Foundation, either version 2 of the
 12+// License, or (at your option) any later version.
 13+//
 14+// emCORE is distributed in the hope that it will be useful,
 15+// but WITHOUT ANY WARRANTY; without even the implied warranty of
 16+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 17+// See the GNU General Public License for more details.
 18+//
 19+// You should have received a copy of the GNU General Public License along
 20+// with emCORE. If not, see <http://www.gnu.org/licenses/>.
 21+//
 22+//
 23+
 24+
 25+#define WIN32_LEAN_AND_MEAN
 26+#include <windows.h>
 27+#include <stdbool.h>
 28+#include <inttypes.h>
 29+
 30+
 31+struct controlreq {
 32+ uint8_t bmRequestType;
 33+ uint8_t bRequest;
 34+ uint16_t wValue;
 35+ uint16_t wIndex;
 36+ uint16_t wLength;
 37+ unsigned char data[];
 38+};
 39+
 40+
 41+extern char dfuimage[];
 42+extern uint32_t dfuimage_size;
 43+
 44+
 45+void print_last_error(char* text, bool force)
 46+{
 47+ DWORD dw = GetLastError();
 48+ if (!dw && !force) return;
 49+ LPVOID lpMsgBuf;
 50+ FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
 51+ NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL);
 52+ printf("%s: Error %d: %s\n", text, dw, lpMsgBuf);
 53+}
 54+
 55+int control(HANDLE dfu, struct controlreq* req, int size, bool silent)
 56+{
 57+ DWORD count;
 58+ OVERLAPPED overlapped;
 59+ memset(&overlapped, 0, sizeof(overlapped));
 60+ SetLastError(0);
 61+ overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
 62+ DeviceIoControl(dfu, 0x2200A0, req, size, req, size, NULL, &overlapped);
 63+ WaitForSingleObject(overlapped.hEvent, 1000);
 64+ DWORD rc = GetOverlappedResult(dfu, &overlapped, &count, FALSE);
 65+ CloseHandle(overlapped.hEvent);
 66+ if (rc <= 0)
 67+ {
 68+ print_last_error("DeviceIoControl", true);
 69+ count = -1;
 70+ CancelIo(dfu);
 71+ if (!silent) MessageBox(0, "DFU transfer failed!", "Error", MB_OK);
 72+ }
 73+ return count;
 74+}
 75+
 76+int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
 77+{
 78+ SetLastError(0);
 79+ HANDLE dfu = CreateFile("\\\\?\\usb#vid_05ac&pid_1225#87200000000001#{b8085869-feb9-404b-8cb1-1e5c14fa8c54}\\0001",
 80+ GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
 81+ if (!dfu || dfu == (HANDLE)-1)
 82+ {
 83+ print_last_error("CreateFile", true);
 84+ MessageBox(0, "Could not open DFU device!", "Error", MB_OK);
 85+ return 1;
 86+ }
 87+ unsigned int buf[514];
 88+ struct controlreq* req = (struct controlreq*)buf;
 89+ int i, j;
 90+ for (i = 0; i < 256; i++)
 91+ {
 92+ buf[i] = i;
 93+ for (j = 0; j < 8; j++)
 94+ {
 95+ if (buf[i] & 1) buf[i] = (buf[i] >> 1) ^ 0xedb88320;
 96+ else buf[i] >>= 1;
 97+ }
 98+ }
 99+ unsigned char* ptr = dfuimage;
 100+ int left = dfuimage_size + 4;
 101+ int packet = 0;
 102+ dfuimage_size = 0xffffffff;
 103+ for (i = 0; i < left - 4; i++)
 104+ dfuimage_size = (dfuimage_size >> 8) ^ buf[(dfuimage_size ^ ptr[i]) & 0xff];
 105+ while (left)
 106+ {
 107+ int size = left > 2048 ? 2048 : left;
 108+ req->bmRequestType = 0x21;
 109+ req->bRequest = 1;
 110+ req->wValue = packet;
 111+ req->wIndex = 0;
 112+ req->wLength = size;
 113+ memcpy(req->data, ptr, size);
 114+ if (control(dfu, req, size + 8, false) != size + 8) return 2;
 115+ req->data[4] = 0;
 116+ while (req->data[4] != 5)
 117+ {
 118+ req->bmRequestType = 0xa1;
 119+ req->bRequest = 3;
 120+ req->wValue = 0;
 121+ req->wIndex = 0;
 122+ req->wLength = 6;
 123+ req->data[4] = 0;
 124+ if (control(dfu, req, 14, false) != 14) return 3;
 125+ }
 126+ ptr += size;
 127+ left -= size;
 128+ packet++;
 129+ }
 130+ req->bmRequestType = 0x21;
 131+ req->bRequest = 1;
 132+ req->wValue = packet;
 133+ req->wIndex = 0;
 134+ req->wLength = 0;
 135+ if (control(dfu, req, 8, false) != 8) return 4;
 136+ int timeout = 20;
 137+ req->data[4] = 0;
 138+ while (req->data[4] != 2 && timeout-- > 0)
 139+ {
 140+ Sleep(100);
 141+ req->bmRequestType = 0xa1;
 142+ req->bRequest = 3;
 143+ req->wValue = 0;
 144+ req->wIndex = 0;
 145+ req->wLength = 6;
 146+ req->data[4] = 0;
 147+ if (control(dfu, req, 14, true) != 14) break;
 148+ }
 149+ if (req->data[4] == 2)
 150+ {
 151+ printf("DFU payload was rejected with code: %02X %02X\n", req->data[4], req->data[0]);
 152+ MessageBox(0, "DFU payload was rejected!", "Error", MB_OK);
 153+ }
 154+ else MessageBox(0, "UMSboot has been launched!", "Success", MB_OK);
 155+ return 0;
 156+}
Index: tools/itunesdfu/bootstrap_ipodnano4g_itunes/version.h
@@ -0,0 +1,36 @@
 2+//
 3+//
 4+// Copyright 2011 TheSeven
 5+//
 6+//
 7+// This file is part of emCORE.
 8+//
 9+// emCORE is free software: you can redistribute it and/or
 10+// modify it under the terms of the GNU General Public License as
 11+// published by the Free Software Foundation, either version 2 of the
 12+// License, or (at your option) any later version.
 13+//
 14+// emCORE is distributed in the hope that it will be useful,
 15+// but WITHOUT ANY WARRANTY; without even the implied warranty of
 16+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 17+// See the GNU General Public License for more details.
 18+//
 19+// You should have received a copy of the GNU General Public License along
 20+// with emCORE. If not, see <http://www.gnu.org/licenses/>.
 21+//
 22+//
 23+
 24+
 25+#ifndef __VERSION_H__
 26+#define __VERSION_H__
 27+
 28+
 29+#define VERSION "0.1.0"
 30+#define VERSION_MAJOR 0
 31+#define VERSION_MINOR 1
 32+#define VERSION_PATCH 0
 33+#define VERSION_SVN "$REVISION$"
 34+#define VERSION_SVN_INT $REVISIONINT$
 35+
 36+
 37+#endif
\ No newline at end of file
Index: tools/itunesdfu/bootstrap_ipodnano4g_itunes/SOURCES
@@ -0,0 +1,2 @@
 2+main.c
 3+resources.S
Index: tools/itunesdfu/bootstrap_ipodnano4g_itunes/resources.S
@@ -0,0 +1,30 @@
 2+//
 3+//
 4+// Copyright 2011 TheSeven
 5+//
 6+//
 7+// This file is part of emCORE.
 8+//
 9+// emCORE is free software: you can redistribute it and/or
 10+// modify it under the terms of the GNU General Public License as
 11+// published by the Free Software Foundation, either version 2 of the
 12+// License, or (at your option) any later version.
 13+//
 14+// emCORE is distributed in the hope that it will be useful,
 15+// but WITHOUT ANY WARRANTY; without even the implied warranty of
 16+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 17+// See the GNU General Public License for more details.
 18+//
 19+// You should have received a copy of the GNU General Public License along
 20+// with emCORE. If not, see <http://www.gnu.org/licenses/>.
 21+//
 22+//
 23+
 24+
 25+.section .data
 26+.global _dfuimage
 27+_dfuimage:
 28+.incbin DFUIMAGE
 29+.global _dfuimage_size
 30+_dfuimage_size:
 31+.long . - _dfuimage
Index: tools/itunesdfu/bootstrap_ipodnano4g_itunes
Property changes on: tools/itunesdfu/bootstrap_ipodnano4g_itunes
___________________________________________________________________
Added: svn:ignore
## -0,0 +1 ##
 32+build