<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.0">Jekyll</generator><link href="/feed.xml" rel="self" type="application/atom+xml" /><link href="/" rel="alternate" type="text/html" /><updated>2021-08-25T13:12:35+00:00</updated><id>/feed.xml</id><title type="html">pofHQ</title><subtitle>Pau Oliva Fora's personal blog.</subtitle><entry><title type="html">SSF2T: The quest for the perfect training mode</title><link href="/2014/04/22/ssf2t-the-quest-for-the-perfect-training-mode/" rel="alternate" type="text/html" title="SSF2T: The quest for the perfect training mode" /><published>2014-04-21T23:37:29+00:00</published><updated>2014-04-21T23:37:29+00:00</updated><id>/2014/04/22/ssf2t-the-quest-for-the-perfect-training-mode</id><content type="html" xml:base="/2014/04/22/ssf2t-the-quest-for-the-perfect-training-mode/">&lt;p&gt;Since I started playing ST a few months ago I wanted to have a training mode on MAME like the one available on the Sega Dreamcast port. I collected all the available training mode cheats but none of them convinced me, so I studied all them, fixed some of the glitches and finally end up taking the best of each one to write my own.&lt;/p&gt;

&lt;p&gt;The first usable training mode cheat was made by Pasky, he explained it very well in &lt;a href=&quot;http://forums.shoryuken.com/discussion/comment/5141353/#Comment_5141353&quot;&gt;this comment&lt;/a&gt;, the main problem with his cheat is that the stun meter stops working properly after the health bar is recharged and the cheat has to disable stun, so the players never get dizzy. The cheat is interesting because he hooks the game code to make it jump into his own subroutine that refills the health bar for both players. This cheat only recharges the bars after hit damage, but not after throw damage.&lt;/p&gt;

&lt;p&gt;After that, there was a &lt;a href=&quot;http://www.mamecheat.co.uk/forums/viewtopic.php?f=4&amp;amp;t=4103#p13288&quot;&gt;new training mode&lt;/a&gt; cheat made by d9x/dammit. It is way easier because it only recharges the health bar when the dummies are at a certain state (for example after being hit). We can see the value of this state at memory address FF8451 (or 0x400 more for P2). This allows the stun meters to work properly, so characters can get dizzy normally, however it still has some glitches like sometimes when the health is recharged opponent gets hit or pushed back.&lt;/p&gt;

&lt;p&gt;In both Pasky and d9x cheats, the health bar can’t be empty (if this happens the round will end). This is one of the things that bothered me, because you can’t get an idea of how much damage you would do, for example with a 5-hit combo, as the bar is refilled very quickly after every hit or when it reaches a certain value.&lt;/p&gt;

&lt;p&gt;Not long ago, &lt;a href=&quot;http://forums.shoryuken.com/discussion/comment/8699161/#Comment_8699161&quot;&gt;jedpossum published a new training mode&lt;/a&gt;, which has the particularity that even after the health bar is empty, the game continues. However it has a few new glitches, like the player’s vertical position after a wall throw is sometimes messed up, and there’s the K.O. slowdown present when the health bar reaches zero.&lt;br /&gt;
&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;So I took the ability to reach an empty health bar from jedpossum’s cheat, and the memory position to control the player state from d9x cheat and created my own training mode cheat, here is an explanation of the memory values I used:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ff8451&lt;/code&gt;: player state, a value of 0 means standing, a value of 2 means crouching, a value of 0xE means after a hit, a value of 0x14 means after a throw, etc..&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ff8478&lt;/code&gt;: the real health value for PL1, range from 0 to 0x90, set to ffff when the round ends.&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ff847a&lt;/code&gt;: a copy of the health value for PL1, range from 0 to 0x90, it’s not modified when the round ends.&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ff860a&lt;/code&gt;: the health bar meter HUD display value, range from 0 to 0x90.&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4c94,4a78,4db0,be64e,bebae&lt;/code&gt;: when the round ends, the game engine will take the value from here (0xffff) and change the player’s health value at ff8478 with that.&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FF82F2&lt;/code&gt;: Slowdown value, when set to 0 there’s no slowdown.&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FF84AD, FF84AE, FF84AB&lt;/code&gt;: stun meter, stun counter, dizzy meter&lt;/p&gt;

&lt;p&gt;Basically what my cheat does is recharge health when both players are idle (with a “timer” so we can delay it, useful if we want to look at how much damage our last hit has done). It will also recharge health when the round is about to end, in two different ways:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;when the player is hit or thrown, and his energy is less than 0xf&lt;/li&gt;
  &lt;li&gt;when the round has actually ended, preventing it to really end&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If the last hit (or throw) does less damage than 0xf, the energy will be recharged before the round actually really ends. If the last hit (or throw) does more damage than 0xf the round will “end”, but we disable the K.O. slowdown and recharge the health really quick so the round continues and no noticeable slowdown happens.&lt;/p&gt;

&lt;p&gt;Here you can see the training mode in action:&lt;/p&gt;
&lt;iframe title=&quot;YouTube video player&quot; width=&quot;960&quot; height=&quot;750&quot; src=&quot;https://www.youtube.com/embed/zmpvOeM1CCU?rel=0&amp;amp;hd=1&quot; frameborder=&quot;0&quot; allowfullscreen=&quot;&quot;&gt;&lt;/iframe&gt;

&lt;p&gt;Another thing that the Dreamcast training mode has is that you can force the PL2 dummy to do a certain action, for example always crouch or jump. This is useful for practicing some combos and can be done with macros using mame-rr, but &lt;a href=&quot;https://twitter.com/angealbertini&quot;&gt;Ange Albertini&lt;/a&gt; suggested me to do it on a mame cheat, so I thought &lt;em&gt;challenge accepted&lt;/em&gt;, and I also wrote a complementary cheat to control the dummy’s repetitive movement… so it’s even better than the dreamcast training mode now :)&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2014/04/ssf2xj-pof-training-mode.png&quot; alt=&quot;super turbo training mode&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This time I’ve decided to share my &lt;a href=&quot;https://github.com/poliva/ssf2xj/blob/master/ssf2xj.xml&quot;&gt;cheat file&lt;/a&gt; on github, instead of publishing all the cheats separately, so &lt;a href=&quot;https://raw.githubusercontent.com/poliva/ssf2xj/master/ssf2xj.xml&quot;&gt;grab it&lt;/a&gt; and if you try my new training mode let me know what do you think in the comments!&lt;/p&gt;

&lt;p&gt;Enjoy! :)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;UPDATE: New download bundle with mame-rr, training mode cheat, hitbox viewer, input display and Sako training. All you need to do is put the roms (ssf2.zip ssf2t.zip and ssf2xj.zip in the “roms” folder):&lt;/strong&gt;&lt;/p&gt;

&lt;h1 id=&quot;download&quot;&gt;&lt;a href=&quot;https://drive.google.com/uc?export=download&amp;amp;id=0B1vYN8cImxr9Z2NRdzV0RUI5OXc&quot;&gt;DOWNLOAD&lt;/a&gt;&lt;/h1&gt;</content><author><name></name></author><category term="SuperTurbo" /><category term="mame" /><category term="ssf2x" /><category term="ssf2xj" /><category term="streetfighter" /><category term="superturbo" /><summary type="html">Since I started playing ST a few months ago I wanted to have a training mode on MAME like the one available on the Sega Dreamcast port. I collected all the available training mode cheats but none of them convinced me, so I studied all them, fixed some of the glitches and finally end up taking the best of each one to write my own.</summary></entry><entry><title type="html">Hacking Super Street Fighter II Turbo (Part 2)</title><link href="/2014/04/08/hacking-super-street-fighter-ii-turbo-part-2/" rel="alternate" type="text/html" title="Hacking Super Street Fighter II Turbo (Part 2)" /><published>2014-04-08T06:08:34+00:00</published><updated>2014-04-08T06:08:34+00:00</updated><id>/2014/04/08/hacking-super-street-fighter-ii-turbo-part-2</id><content type="html" xml:base="/2014/04/08/hacking-super-street-fighter-ii-turbo-part-2/">&lt;p&gt;In today’s post I will try to illustrate the difference between a RAM cheat and a ROM cheat. RAM cheats usually change the data the game has in RAM, for example the &lt;a href=&quot;/2014/03/25/hacking-super-street-fighter-ii-turbo-part-1/&quot;&gt;previous post&lt;/a&gt; showed how to change the value in a fixed memory address to adjust the game difficulty during gameplay. ROM cheats patch the game’s program code to force the game engine take a different path.&lt;/p&gt;

&lt;p&gt;One thing I’ve always wanted to see is the combo messages that appear on the side of the screen when you do a multiple hit combo, but for the combos that the CPU does, which for some reason don’t appear. So, that’s what I’ll show you how to do today: hack the ST rom to see the CPU combo messages, plus some other bonus cheats we’ll discover while getting there :)&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2014/04/ssf2xj-cpu-combo-msg.jpg&quot; alt=&quot;ssf2xj-cpu-combo-msg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;First thing we want to do is locate the memory region or address where the game stores who controls a character, the CPU or a human player. From there, we’ll see where in the code this memory region is accessed, and that should lead us to some point where the game engine decides “it’s a human player so I will show the combo message, or it’s a computer so I will not show it”. What we will try to do is patch that part of the code to make the game engine always show it!&lt;/p&gt;

&lt;p&gt;We can start working on the “CPU Combo Messages” cheat using the &lt;em&gt;memdump &amp;amp; diff&lt;/em&gt; method we used in the &lt;a href=&quot;/2014/03/25/hacking-super-street-fighter-ii-turbo-part-1/&quot;&gt;previous example&lt;/a&gt;, but we’ll use a different method now just for the purpose of illustrating the possibilities of MAME’s built in debugger: the “cheat” commands.&lt;/p&gt;

&lt;p&gt;Start the game with the debugger enabled, and start playing with 2 human controlled characters (P1 &amp;amp; P2). When the “Round 1” message disappears press ENTER in the debugger screen to break, and type the command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cheatinit&lt;/code&gt;. This will start a new cheat search in memory by &lt;em&gt;remembering&lt;/em&gt; the state of all memory addresses at that point. Now return into the game, and start playing with one human player (P1) against the CPU (P2). Ideally that match should have the same characters as the one before, with the same colors, etc… to produce the lesser variations possible in the game’s memory. Now when the “Round 1” message disappears, press ENTER again in the debugger screen and type the command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cheatnext decrease,1&lt;/code&gt;: this will search for all bytes that have decreased by one since we did the &lt;em&gt;cheatinit&lt;/em&gt;. Now we can do a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cheatlist&lt;/code&gt; to see all the possible memory locations that have changed:&lt;br /&gt;
&lt;!--more--&gt;&lt;br /&gt;
&lt;img src=&quot;/assets/images/2014/04/ssf2xj-cheatlist.png&quot; alt=&quot;ssf2xj cheatlist&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If all went well, one of those memory locations listed here should indicate the game whether the P2 is controlled by a computer or by a human. Now we have to try each one to try to find which is the correct one, there are a few possibilities to do this, you can press ALT+M and change them manually in the memory editor window, or you can change them in the debugger using the command line like this:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2014/04/ssf2xj-modifymem.png&quot; alt=&quot;ssf2xj modify memory&quot; /&gt;&lt;/p&gt;

&lt;p&gt;But the easiest one is to have the debugger generate a cheat file with all the possibilities for us, using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cheatlist ssf2xj.xml&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;If you are playing against the CPU, you’ll see that immediately after typing the command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pb@FF8BDC=01&lt;/code&gt; the CPU stops moving, and you can control the character using the P2 controls. So we have now found the exact memory location that tells the game whether we’re playing against the CPU (memory value equals 0) or against a human (memory value equals 1).&lt;/p&gt;

&lt;p&gt;BONUS: we can use that information we have just discovered to write a “Controller Mode” cheat, that should allow us to transfer the control of a cpu-controlled character to a human-controlled character and vice versa. The controller mode cheat could initially be something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://gist.github.com/10100281&quot;&gt;https://gist.github.com/10100281&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note that in this cheat I’ve put the address also for P1 (0xFF87DC). You could have used the same method to find it, but it is well known that in Super Turbo ROM the offset for the player 2 base address is 0x400 bytes after the player 1, so to find the P1 address I just subtracted 0x400 to the P2 address we already found using the debugger.&lt;/p&gt;

&lt;p&gt;Now it’s time to test the cheat. You’ll notice that you can properly transfer a CPU-controlled character to a human-controlled character anywhere, but when you want to do it the other way round (human to CPU) the game crashes and restarts itself if we are in the middle of a round (you can do it only before starting the match, for example in the character selection screen). We’ll see a possible workaround for that later, but now we can continue to pursue our initial goal.&lt;/p&gt;

&lt;p&gt;Let’s go back to MAME’s debugger and put a few watchpoints at the memory address for player one and player two, one will tell us when the memory address is read by the CPU, the other will tell us when the value is changed (write). We will also print the program counter (PC) when the read/write operation occurs, and resume the execution:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wpset 0xFF87DC,1,r,1,{printf &quot;P1 Read @ %X=%X with PC=%X&quot;, wpaddr, pb@FF87DC, PC; go}
wpset 0xFF87DC,1,w,1,{printf &quot;P1 Write @ %X=%X with PC=%X&quot;, wpaddr, pb@FF87DC, PC; go}
wpset 0xFF8BDC,1,r,1,{printf &quot;P2 Read @ %X=%X with PC=%X&quot;, wpaddr, pb@FF8BDC, PC; go}
wpset 0xFF8BDC,1,w,1,{printf &quot;P2 Write @ %X=%X with PC=%X&quot;, wpaddr, pb@FF8BDC, PC; go}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;With these commands we get the following output during gameplay:&lt;br /&gt;
&lt;img src=&quot;/assets/images/2014/04/ssf2xj-watchpoints-p1p2.png&quot; alt=&quot;ssf2xj watchpoints p1 p2&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As we can see, there’s a pattern here where the memory position indicating if the character is CPU or human controlled is read always when the program counter is always at the same 6 instructions. We can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bpset&lt;/code&gt; to set breakpoints or remove the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;go&lt;/code&gt; command at the end of the previous watchpoints to stop execution when the CPU is at these instructions, so that we will be able to inspect the disassembly. We can also use the debugger’s built-in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dasm&lt;/code&gt; command to write the disassembled code into a file:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;dasm 1.asm,0x597a-20,100
dasm 2.asm,0x78034-20,100
dasm 3.asm,0x77E76-20,100
dasm 4.asm,0x68f58-20,100
dasm 5.asm,0xbe54e-20,100
dasm 6.asm,0xbe56e-20,100
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Upon inspection of the disassembled code we can see that we mostly have BEQ and BNE instructions. I’ll explain the basic concepts to be able to NOP an instruction or invert a branch condition using very basic &lt;a href=&quot;http://en.wikipedia.org/wiki/Motorola_68000&quot;&gt;motorola 68000&lt;/a&gt; assembly concepts, which is the main CPU that powers the CPS-2 boards:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The instruction NOP (no operation) has the opcode &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NOP = 0x4e71&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;The instruction BEQ (Branch if Equal) has the opcode &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BEQ = 0x67XXYYYYZZZZ&lt;/code&gt; where XXYYYYZZZZ indicates how far we will jump forward if the previous comparison instruction (usually a TST) was found to be equal.&lt;/li&gt;
  &lt;li&gt;The instruction BNE (Branch if Not Equal) has the opcode &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BNE = 0x66XXYYYYZZZZ&lt;/code&gt; where XXYYYYZZZZ indicates how far we will jump forward if the previous comparison instruction (usually a TST) was not equal.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So if we need to invert the logic we can change the BEQ for BNE by swapping a 67 for a 66 on the first byte of the opcode, or if we want to always force a certain code path we can just NOP the branch instruction and it will always go to the next instruction right after the NOP’ed branch.&lt;/p&gt;

&lt;p&gt;Now that we have this basic mortola 68k code patching introduction, if we just print the opcodes at the locations we have found using the debugger we will obtain the following results:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2014/04/ssf2xj-opcodes.png&quot; alt=&quot;ssf2xj opcodes&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As we can see, the first byte of every opcode is always 66 or 67, meaning it’s a BNE or BEQ instruction. So we can either NOP, or invert the condition on every of these locations and see what happens during gameplay to get an idea of what the code is doing there. If we want to inspect all the details of the code it is better to use an external disassembler; we can use the command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;save&lt;/code&gt; to dump the decrypted CPS-2 opcodes into a file, and load this file in a regular disassembler like &lt;a href=&quot;http://radare.org/&quot;&gt;radare2&lt;/a&gt; or IDA Pro.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2014/04/ssf2xj-radare.png&quot; alt=&quot;ssf2xj-radare&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As we can see here (it’s easier to see if we also trace it with the debugger when analyzing the code), the BEQ instruction at 0x68f58 will skip the next 6 instructions if the values in the registers indicate that we’re checking the computer and not a human player. If we skip these instructions, the combo message will not appear, so our goal is not to skip them. If we invert the condition (convert the BEQ in a BNE), the message will only show for the computer and not for the human player, and that’s not what we want. If we want the message to show up always regardless if the character is computer or human controlled, we have to NOP the branch instruction, to make sure the instructions after it will always be executed. The branch instruction at 0x68f58 has only 2 bytes, so to NOP it we just need to enter the command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ow@68F58=4E71&lt;/code&gt;. Now we’re ready to write the mame cheat for that:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://gist.github.com/10099906&quot;&gt;https://gist.github.com/10099906&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Remember about the “bonus” controller mode cheat we did before?, it was done by just changing a value in memory (RAM cheat), but now that we know every place of the code where this value is read, we are in disposition to do a ROM cheat for that! So with this simple cheat (again, just NOP’ing an instruction), we can transfer a CPU controlled character to a human controlled character:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://gist.github.com/10100556&quot;&gt;https://gist.github.com/10100556&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Enjoy :)&lt;/p&gt;</content><author><name></name></author><category term="SuperTurbo" /><category term="mame" /><category term="ssf2x" /><category term="ssf2xj" /><category term="streetfighter" /><category term="superturbo" /><summary type="html">In today’s post I will try to illustrate the difference between a RAM cheat and a ROM cheat. RAM cheats usually change the data the game has in RAM, for example the previous post showed how to change the value in a fixed memory address to adjust the game difficulty during gameplay. ROM cheats patch the game’s program code to force the game engine take a different path.</summary></entry><entry><title type="html">Hacking Super Street Fighter II Turbo (Part 1)</title><link href="/2014/03/25/hacking-super-street-fighter-ii-turbo-part-1/" rel="alternate" type="text/html" title="Hacking Super Street Fighter II Turbo (Part 1)" /><published>2014-03-24T23:49:00+00:00</published><updated>2014-03-24T23:49:00+00:00</updated><id>/2014/03/25/hacking-super-street-fighter-ii-turbo-part-1</id><content type="html" xml:base="/2014/03/25/hacking-super-street-fighter-ii-turbo-part-1/">&lt;p&gt;In this post I will show how to debug the Super Street Fighter II Turbo ROM in MAME, to create a simple cheat. This will (hopefully) be the first post of a series that will show more advanced  use of the MAME debugger and dig deeper into reverse engineering Super Turbo.
First we need to launch MAME using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-debug&lt;/code&gt; parameter, this will launch the MAME debugger. You can use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;help&lt;/code&gt; command in the debugger to see the help.
&lt;img src=&quot;/assets/images/2014/03/mame-debugger.png&quot; alt=&quot;mame debugger&quot; /&gt;&lt;/p&gt;

&lt;!--more--&gt;
&lt;p&gt;For this first example we’ll create a cheat to change the game difficulty on the fly. To do this, we’ll press F5 (or type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;go&lt;/code&gt; in the debugger console) and we’ll press F2 (the “test switch” key) to access the game’s Test Menu. In the test menu, under “System Configuration” we can change the settings of the game, one of those settings is the Game Difficulty, ranging from 1/Easieast to 8/Hardest.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2014/03/ssf2xj-system-configuration-menu.png&quot; alt=&quot;ssf2xj-system-configuration-menu&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now we need to discover what changes in the game’s memory when we change that setting. To do this we’ll use the debugger’s ‘&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dump&lt;/code&gt;’ command, that dumps program memory as text. We can find the right syntax for the ‘&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dump&lt;/code&gt;’ command using the debugger’s command ‘&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;help memory&lt;/code&gt;’. It expects a filename as first argument, the start address and the length of the memory portion we want to dump. So we’ll put the Game Difficulty setting to 1/Easiest and issue the following dump command: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dump easiest.txt,0,0xffffff&lt;/code&gt;, then we’ll set the Game difficulty setting to 8/Hardest and repeat the command: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dump hardest.txt,0,0xffffff&lt;/code&gt;. Now we will use the diff utility to see the difference between both files:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ diff -urN easiest.txt hardest.txt
--- easiest.txt	2014-03-25 00:39:10.038344425 +0100
+++ hardest.txt	2014-03-25 00:39:24.222343789 +0100
@@ -593047,7 +593047,7 @@
 90C960:  0020 0000 0020 0000 0020 0000 0050 0017  . ... ... ...P..
 90C970:  0045 0017 0020 001B 0020 0000 0020 0000  .E... ... ... ..
 90C980:  0020 0000 0020 0000 0020 0000 0043 000D  . ... ... ...C..
-90C990:  0020 0000 0031 0003 0020 0000 0031 001B  . ...1... ...1..
+90C990:  0020 0000 0031 0003 0020 0000 0038 001B  . ...1... ...8..
 90C9A0:  0020 0000 0053 0003 0020 0000 004F 0003  . ...S... ...O..
 90C9B0:  0020 0000 004F 0003 0020 0000 004F 0003  . ...O... ...O..
 90C9C0:  0020 0000 004F 0003 0020 0000 0054 001B  . ...O... ...T..
@@ -1046571,7 +1046571,7 @@
 FF82A0:  0000 0000 0000 0000 0000 0000 0000 0101  ................
 FF82B0:  0000 0000 0000 0101 0000 0000 02C0 0000  ................
 FF82C0:  0000 C000 0000 0000 0000 0000 0000 0000  ................
-FF82D0:  C91C 0000 0000 0000 0000 0100 0000 0000  ................
+FF82D0:  C91C 0007 0000 0000 0000 0100 0000 0000  ................
 FF82E0:  0000 0000 0100 0100 0000 0000 0000 0000  ................
 FF82F0:  0000 0000 0000 0000 0000 0000 0000 0000  ................
 FF8300:  0000 0000 0000 0000 0000 0000 0000 0000  ................
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can see two values have changed, one at position 0x90C99D and the other at position 0xFF82D3. The first one is the probably the text displayed on the screen, while the second one is probably the position in memory where the difficulty value is stored, as we can guess it ranges from 0 (Easiest) to 7 (Hardest). To confirm our guess we’ll launch the Memory View window in MAME’s debugger, to do this just press CTRL+M and the Memory View window will appear. Type in the memory address of our previous finding and then switch the Game Difficulty in the System Configuration Menu to see if the memory value at 0xFF82D3 changes when we change the difficulty.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2014/03/ssf2xj-memory.gif&quot; alt=&quot;ssf2xj memory&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It appears that our guess was right, and we have found the place in memory where the Game Difficulty setting is stored. Now let’s do the MAME cheat to be able to change this setting on the fly during gameplay, so we can adjust the difficulty while playing against the CPU.&lt;/p&gt;

&lt;p&gt;The MAME cheat system XML format is explained in the comments on the &lt;a href=&quot;http://mamedev.org/source/src/emu/cheat.c.html&quot;&gt;src/emu/cheat.c&lt;/a&gt; file inside MAME source code. We have to create a cheat file inside MAME’s “cheat” folder named the same as the ROM zip file but with xml extension, so for Super Street Fighter 2X (Japanese version of Super Turbo), the filename will be “ssf2xj.xml”. First we open the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mamecheat&lt;/code&gt; tag, then we open the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cheat&lt;/code&gt; tag and add the description for example “Select Difficulty”. Then we will use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parameter&lt;/code&gt; tag to let the user chose the difficulty using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;item&lt;/code&gt; values from 0 to 7. Then we’ll use a temporary variable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;temp0&lt;/code&gt; to store the current value of the Game Difficulty when the cheat starts (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;script state=&quot;on&quot;&lt;/code&gt;), we’ll use this variable to restore the value to the user’s setting when the cheat ends (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;script state=&quot;off&quot;&lt;/code&gt;). While the cheat is being run (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;script state=&quot;run&quot;&lt;/code&gt;) we’ll just overwrite the memory value at address 0xFF82D3 with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;param&lt;/code&gt; value selected in the cheat menu. The final cheat code looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://gist.github.com/10100976&quot;&gt;https://gist.github.com/10100976&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So now using our newly created cheat we can adjust the difficulty during gameplay against the CPU by accessing the cheats menu (press TAB on MAME, it must be started with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-cheat&lt;/code&gt; option):&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2014/03/ssf2xj-cheats-menu.png&quot; alt=&quot;ssf2xj cheats menu&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Enjoy :)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;UPDATE:&lt;/strong&gt; As &lt;a href=&quot;http://forums.shoryuken.com/discussion/comment/8743741/#Comment_8743741&quot;&gt;jedpossum points out&lt;/a&gt;, the following memory map of the CPS2 would have helped to decide what needed to be changed instead of guessing (thank you!):&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;CPS2:
0x000000 - 0x3FFFFF Main Program
0x400000 - 0x40000A Encryption (aka the battery memory on pheonixed roms it's 0xFFFFF0 - 0xFFFFFA)
0x618000 - 0x619FFF Shared ram for the Z80 aka tells what sfx or music to play.
0x660000 - 0x663FFF Network Memory
0x900000 Start of Graphic memory (can change with each game)
	
ST:
0x900000 - 0x903FFF Palette
0x904000 - 0x907FFF 16x16
0x908000 - 0x90BFFF 32x32
0x90C000 - 0x90FFFF 8x8
0x910000 - 0x913FFF 16x16 mainly hud and character names on select screen
	
0xFF0000 - 0xFFFFFF Main Memory
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><author><name></name></author><category term="SuperTurbo" /><category term="mame" /><category term="ssf2x" /><category term="ssf2xj" /><category term="streetfighter" /><category term="superturbo" /><summary type="html">In this post I will show how to debug the Super Street Fighter II Turbo ROM in MAME, to create a simple cheat. This will (hopefully) be the first post of a series that will show more advanced use of the MAME debugger and dig deeper into reverse engineering Super Turbo. First we need to launch MAME using the -debug parameter, this will launch the MAME debugger. You can use the help command in the debugger to see the help.</summary></entry><entry><title type="html">ZTE Open FirefoxOS Phone, root and first impressions</title><link href="/2013/07/05/zte-open-firefoxos-phone-root-and-first-impressions/" rel="alternate" type="text/html" title="ZTE Open FirefoxOS Phone, root and first impressions" /><published>2013-07-05T12:32:23+00:00</published><updated>2013-07-05T12:32:23+00:00</updated><id>/2013/07/05/zte-open-firefoxos-phone-root-and-first-impressions</id><content type="html" xml:base="/2013/07/05/zte-open-firefoxos-phone-root-and-first-impressions/">&lt;p&gt;&lt;img src=&quot;/assets/images/2013/07/zte-open.jpg&quot; alt=&quot;zte open&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ZTE Open&lt;/strong&gt; is the first non-developer &lt;strong&gt;FirefoxOS&lt;/strong&gt; phone, sold commercially in Spain by Movistar.&lt;/p&gt;

&lt;p&gt;It can be rooted using &lt;a href=&quot;https://www.codeaurora.org/projects/security-advisories/multiple-issues-diagkgsl-system-call-handling-cve-2012-4220-cve-2012&quot;&gt;CVE-2012-4220 aka Qualcomm DIAG root&lt;/a&gt; discovered by Giantpune. This security advisory was released by Qualcomm on November 15, 2012. The ZTE Open has been launched commercially 7 months later and neither ZTE nor Movistar have bothered to patch this security hole, shame on them for selling vulnerable devices to customers.&lt;/p&gt;

&lt;p&gt;The ZTE Open comes with kernel 3.0.8 which is also vulnerable to &lt;a href=&quot;http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2013-2094&quot;&gt;CVE-2013-2094 (perf_event) exploit&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;root&quot;&gt;Root&lt;/h3&gt;

&lt;p&gt;I took the &lt;a href=&quot;https://github.com/hiikezoe/break_setresuid&quot;&gt;exploit by Hiroyuki Ikezoe&lt;/a&gt; and adapted it to work on the ZTE Open. The source code is available &lt;a href=&quot;https://github.com/poliva/root-zte-open&quot;&gt;here&lt;/a&gt;, and a &lt;em&gt;redy-to-use&lt;/em&gt; compiled exploit is here: &lt;a href=&quot;/archives/files/root-zte-open.zip&quot;&gt;&lt;strong&gt;DOWNLOAD&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;These are the details of the original firmware, as hopefully ZTE will patch the security hole and this exploit might not work in future versions:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ro.build.display.id=OPEN_FFOS_V1.0.0B04_TME
ro.build.sw_internal_version=B2G_P752D04V1.0.0B08_TME
ro.build.firmware_revision=V1.01.00.01.019.120
ro.build.date=Fri May 31 23:10:17 CST 2013
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To run the exploit connect your phone to your computer using the USB cable, and make sure ‘&lt;em&gt;Remote debugging&lt;/em&gt;’ is enabled on your phone in Settings -&amp;gt; Device information -&amp;gt; More Information -&amp;gt; Developer.&lt;br /&gt;
You need to have the adb binary in your computer’s path, (if you don’t know what ADB is don’t bother rooting your phone) then execute “run.sh” on Linux or OS X, or “run.bat” on Windows.&lt;br /&gt;
If the exploit fails, reboot your ZTE Open and try again (the linux/MAC version will attempt to do that automatically). Once the exploit is successful it will remount the system partition in read/write mode and copy a setuid “su” binary into &lt;em&gt;/system/xbin/su&lt;/em&gt;.&lt;/p&gt;

&lt;h3 id=&quot;custom-roms&quot;&gt;Custom ROMs&lt;/h3&gt;

&lt;p&gt;The bootloader on the ZTE Open does not allow to flash or boot unsigned code through fastboot protocol. The stock recovery image will verify the signature of update packages and not allow you to flash self-signed updates. To overcome that limitation you can flash a custom recovery image that will allow you to backup your current ROM to SD card and flash your customized build of FirefoxOS (or if you want, your own Android port).&lt;/p&gt;

&lt;p&gt;You can download ClockWorkMod recovery for ZTE Open here: &lt;a href=&quot;/archives/files/recovery-clockwork-6.0.3.3-roamer2.img&quot;&gt;recovery-clockwork-6.0.3.3-roamer2.img&lt;/a&gt;.&lt;br /&gt;
To flash it:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# first backup your existing recovery
adb shell dd if=/dev/mtd/mtd0 of=/sdcard/stock-recovery.img bs=4k
adb pull /sdcard/stock-recovery.img
	
# then flash clockworkmod recovery
adb push recovery-clockwork-6.0.3.3-roamer2.img /sdcard/cwm.img
adb shell flash_image recovery /sdcard/cwm.img
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To boot into recovery mode, hold both volume &lt;del&gt;down&lt;/del&gt; up and the power button while powering on the phone.&lt;/p&gt;

&lt;p&gt;Enjoy! :)&lt;/p&gt;</content><author><name></name></author><category term="FirefoxOS" /><category term="CVE-2012-4220" /><category term="DIAG" /><category term="exploit" /><category term="firefoxos" /><category term="roamer2" /><category term="root" /><category term="zte" /><category term="zte-open" /><summary type="html"></summary></entry><entry><title type="html">SSD alignment on linux with ext4 and LVM</title><link href="/2013/01/12/ssd-alignment-on-linux-with-ext4-and-lvm/" rel="alternate" type="text/html" title="SSD alignment on linux with ext4 and LVM" /><published>2013-01-12T00:08:36+00:00</published><updated>2013-01-12T00:08:36+00:00</updated><id>/2013/01/12/ssd-alignment-on-linux-with-ext4-and-lvm</id><content type="html" xml:base="/2013/01/12/ssd-alignment-on-linux-with-ext4-and-lvm/">&lt;p&gt;First make sure to create partitions aligned to your SSD erase block size (in my case 512k):&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo fdisk -H32 -S32 /dev/sdb&lt;/code&gt;&lt;br /&gt;
You can check with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fdisk -lu /dev/sdb&lt;/code&gt; that the start of each partition is divisible by 512.&lt;/p&gt;

&lt;p&gt;Then initialize the desired partition to use with LVM2 using the &lt;tt&gt;dataalignment&lt;/tt&gt; parameter:&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pvcreate --dataalignment 512k /dev/sdb1&lt;/code&gt;&lt;br /&gt;
Make sure your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/lvm/lvm.conf&lt;/code&gt; contains the following options:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;md_chunk_alignment = 1
data_alignment_detection = 1
data_alignment = 0
data_alignment_offset_detection = 1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now you can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vgcreate&lt;/code&gt; to create your volume grup, and then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lvcreate&lt;/code&gt; to create the logical volumes.&lt;/p&gt;

&lt;p&gt;When creating ext4 filesystems (with TRIM support), use the following command:&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mkfs.ext4 -O extent -b 4096 -E stride=128,stripe-width=128 /dev/mapper/vg1-test&lt;/code&gt;&lt;br /&gt;
&lt;tt&gt;stride&lt;/tt&gt; and &lt;tt&gt;stripe-width&lt;/tt&gt; are calculated as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sector size / block size&lt;/code&gt; = 512k / 4k = 128&lt;/p&gt;

&lt;p&gt;When mounting ext4 filesystems, use the ‘&lt;tt&gt;discard&lt;/tt&gt;’ parameter to enable TRIM support:&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mount -o discard,noatime,nodiratime /dev/mapper/vg1-test /mnt/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Extra tip&lt;/strong&gt; : for more speed you can consider turning off journaling (to avoid double-write overhead), at the cost of an easily corruptable filesystem.&lt;br /&gt;
Check if journaling is enabled: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dumpe2fs /dev/mapper/vg1-test |grep 'Filesystem features'&lt;/code&gt;&lt;br /&gt;
Disable journaling: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tune2fs -O ^has_journal&lt;/code&gt;&lt;/p&gt;</content><author><name></name></author><category term="linux" /><category term="align" /><category term="ext4" /><category term="fdisk" /><category term="lvm" /><category term="ssd" /><category term="trim" /><summary type="html">First make sure to create partitions aligned to your SSD erase block size (in my case 512k): sudo fdisk -H32 -S32 /dev/sdb You can check with fdisk -lu /dev/sdb that the start of each partition is divisible by 512.</summary></entry><entry><title type="html">Fortifying a Galaxy Nexus with stock-ish image and root access</title><link href="/2012/07/30/fortifying-a-galaxy-nexus-with-stock-ish-image-and-root-access/" rel="alternate" type="text/html" title="Fortifying a Galaxy Nexus with stock-ish image and root access" /><published>2012-07-30T09:36:43+00:00</published><updated>2012-07-30T09:36:43+00:00</updated><id>/2012/07/30/fortifying-a-galaxy-nexus-with-stock-ish-image-and-root-access</id><content type="html" xml:base="/2012/07/30/fortifying-a-galaxy-nexus-with-stock-ish-image-and-root-access/">&lt;p&gt;&lt;img src=&quot;/assets/images/2012/07/gnex.jpg&quot; alt=&quot;galaxy nexus&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In this post I will describe my recipe to have a Samsung Galaxy Nexus (codename “maguro”) using a rooted factory image, capable of getting OTA updates without loosing root access and with a locked bootloader, keeping the user data safe in case it gets lost or stolen, in the sense that the person getting it will not be able to extract personal details from it like Google accounts, settings, downloaded apps and their data, media, etc.&lt;/p&gt;

&lt;p&gt;I assume the reader starts with a stock unmodified &lt;a href=&quot;https://developers.google.com/android/nexus/images&quot;&gt;factory image&lt;/a&gt;, and knows how to use fastboot.&lt;/p&gt;

&lt;!--more--&gt;

&lt;h3 id=&quot;step-1-getting-root-access&quot;&gt;Step 1: Getting root access&lt;/h3&gt;

&lt;p&gt;The first step is getting root access, to accomplish this the easiest way is to temporarily unlock the bootloader (don’t worry, we will re-lock it later).&lt;/p&gt;

&lt;p&gt;Open a shell prompt and type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fastboot oem unlock&lt;/code&gt;, all data on the phone will be lost as after &lt;tt&gt;oem unlock&lt;/tt&gt; the bootloader performs a factory data reset (also called hard-reset or reset to factory default).&lt;/p&gt;

&lt;p&gt;Once the bootloader is unlocked, we can flash “unsigned” data through fastboot, this allows us to flash a customized recovery image, which will allow to flash an usable ‘su’ binary with the proper suid permissions and a superuser app into the system.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2012/07/clockworkmod.png&quot; alt=&quot;clockworkmod recovery&quot; /&gt;&lt;/p&gt;

&lt;p&gt;There are a few custom recovery images out there to choose from, the most popular being &lt;a href=&quot;http://www.clockworkmod.com/rommanager/&quot;&gt;ClockworkMod recovery&lt;/a&gt; and &lt;a href=&quot;http://teamw.in/project/twrp2/&quot;&gt;TeamWin Recovery Project (TWRP)&lt;/a&gt;. I recommend the later, because it supports decrypting an encrypted data partition on Galaxy Nexus since version 2.2.0.&lt;/p&gt;

&lt;p&gt;To successfully root the device, first flash the custom recovery image using fastboot: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fastboot flash recovery recovery-maguro.img&lt;/code&gt;, then reboot into recovery &lt;strong&gt;without booting the system&lt;/strong&gt; (this is important, as during boot the recovery image checksum is verified and if it doesn’t match the stock recovery the system will overwrite the custom recovery with the stock one).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2012/07/superuser.png&quot; alt=&quot;superuser apk&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Once we’re into the custom recovery, we need to flash the ‘su’ binary and the superuser apk, again there are multiple possibilities out there, the most popular being &lt;a href=&quot;https://play.google.com/store/apps/details?id=com.noshufou.android.su&quot;&gt;ChainsDD Superuser&lt;/a&gt; (&lt;a href=&quot;http://androidsu.com/superuser/&quot;&gt;flashable zip&lt;/a&gt;) and &lt;a href=&quot;https://play.google.com/store/apps/details?id=eu.chainfire.supersu&quot;&gt;Chainfire SuperSU&lt;/a&gt; (&lt;a href=&quot;http://download.chainfire.eu/204/SuperSU/CWM-SuperSU-v0.94.zip&quot;&gt;flashable zip&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Finally, when the phone is booted again the stock recovery will be automatically flashed and you will have a rooted phone with unlocked bootloader and stock recovery.&lt;/p&gt;

&lt;h3 id=&quot;step-2-relocking-the-bootloader&quot;&gt;Step 2: Relocking the bootloader&lt;/h3&gt;

&lt;p&gt;Re-locking the bootloader is important, because with an unlocked bootloader any unauthorized user could access your private data by flashing a custom recovery and backing up or mounting your storage and data partitions. This is why the ‘&lt;tt&gt;oem unlock&lt;/tt&gt;’ process wipes your data, and this is why you should keep the bootloader locked.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2012/07/bootunlocker.png&quot; alt=&quot;bootunlocker&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The Galaxy Nexus bootloader stores the lock status at position 0x000007C (124 decimal) of the &lt;tt&gt;param&lt;/tt&gt; partition of the device’s internal storage. When the byte is set to ‘0’, bootloader is unlocked, when it is set to ‘1’ bootloader is locked. As you can guess now, if you have root access you can manually change the bootloader status from the system, thus it is possible to unlock and relock it without using fastboot, and without wiping your data. You could do this process manually using ‘&lt;tt&gt;dd&lt;/tt&gt;’, but of course the good folks at XDA have created an open source app to automate this process for you. Install &lt;a href=&quot;https://play.google.com/store/apps/details?id=net.segv11.bootunlocker&quot;&gt;BootUnlocker for Galaxy Nexus&lt;/a&gt; from the play store, give it root permissions and re-lock your bootloader now. If you want to have a look at the application source code, check the &lt;a href=&quot;https://code.google.com/p/boot-unlocker-gnex/&quot;&gt;google code project page&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You now have a rooted phone with stock recovery and locked bootloader.&lt;/p&gt;

&lt;h3 id=&quot;step-3-encrypting-the-phone&quot;&gt;Step 3: Encrypting the phone&lt;/h3&gt;

&lt;p&gt;Encryption on Android uses the dm-crypt layer in the Linux kernel, to enable encryption go to Settings, Security, Encryption and click on “Encrypt phone”, for the encryption process to start battery should be fully charged and the phone AC adapter must be plugged in. For more details on how encryption works, read &lt;a href=&quot;http://source.android.com/tech/encryption/android_crypto_implementation.html&quot;&gt;Notes on the implementation of encryption in Android&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2012/07/encrypt.png&quot; alt=&quot;android encryption&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Once the phone is encrypted, you need to type a numeric PIN or password to decrypt it each time you power it on. The master key to decrypt the filesystem is encrypted with a hash of the user’s lock screen password (that’s why you can only use pin or password in the lockscreen when encryption is enabled).&lt;/p&gt;

&lt;p&gt;Currently, there is only one password for both the encryption and lock screen. This is especially bad because you cannot turn off screen lock and therefore have to type it rather frequently, which makes it easier to get a glance on the screen while typing it. Until Google provides a way to use different passwords for encryption and screen lock, you can manually change password for encryption by issuing the following shell command as root:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vdc cryptfs changepw &amp;lt;new_password&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This command changes only encryption password requested at phone boot. The lock screen PIN or password remains unchanged. Please see (and star) &lt;a href=&quot;http://code.google.com/p/android/issues/detail?id=29468&quot;&gt;Android issue 29468&lt;/a&gt; for Google to implement this in the UI.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2012/07/cryptfs.png&quot; alt=&quot;cryptfs password&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Again, there’s an app for that too! check out &lt;a href=&quot;https://play.google.com/store/apps/details?id=org.nick.cryptfs.passwdmanager&quot;&gt;Cryptfs Password&lt;/a&gt; (&lt;a href=&quot;https://github.com/nelenkov/cryptfs-password-manager&quot;&gt;github source&lt;/a&gt;) if you want to change the password without messing with the command line.&lt;/p&gt;

&lt;p&gt;Make sure to choose a good password not based on a dictionary word, as the encryption can be cracked using brute force (see &lt;a href=&quot;https://viaforensics.com/mobile-security/droid-gaining-access-android-user-data.html&quot;&gt;Into the Droid – Gaining Access to Android User Data&lt;/a&gt; presentation from Thomas Cannon at Defcon 2012, &lt;a href=&quot;https://viaforensics.com/wpinstall/wp-content/uploads/into-the-droid-viaForensics-Defcon-2012.026.png&quot;&gt;slide 26&lt;/a&gt; and &lt;a href=&quot;https://viaforensics.com/wpinstall/wp-content/uploads/into-the-droid-viaForensics-Defcon-2012.027.png&quot;&gt;slide 27&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;You have now an encrypted rooted phone with stock recovery and locked bootloader.&lt;/p&gt;

&lt;h3 id=&quot;step-4-keeping-root-access-after-ota-updates&quot;&gt;Step 4: Keeping root access after OTA updates&lt;/h3&gt;

&lt;p&gt;When Over The Air updates are applied, the function &lt;tt&gt;set_perm_recursive&lt;/tt&gt; is called from recovery, this removes the setuid bits on the ‘su’ binary and disables your root access after the OTA has been applied.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2012/07/otarootkeeper.png&quot; alt=&quot;ota root keeper&quot; /&gt;&lt;/p&gt;

&lt;p&gt;To prevent this form happening, one could change the file attributes for the ‘su’ binary to immutable using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chattr +i /system/xbin/su&lt;/code&gt;, again there’s an app that will do this for you automatically (and also keep a backup of your su binary, allowing to “temporarily unroot” your phone): &lt;a href=&quot;https://play.google.com/store/apps/details?id=org.projectvoodoo.otarootkeeper&quot;&gt;OTA RootKeeper&lt;/a&gt;, the source code is &lt;a href=&quot;https://github.com/project-voodoo/ota-rootkeeper-app&quot;&gt;available on github&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The same functionality from OTA RootKeper has been recently included in recent versions of ChainsDD Superuser app.&lt;/p&gt;

&lt;p&gt;You have now an encrypted rooted phone with stock recovery, locked bootloader and capable of keeping root access through OTAs.&lt;/p&gt;

&lt;h3 id=&quot;some-notes-concerning-ota-updates-and-backups&quot;&gt;Some notes concerning OTA updates and backups&lt;/h3&gt;

&lt;p&gt;With this setup, you have not modified any system files so your phone should be able to automatically get OTA updates and apply them cleanly. If you are impatient and want to manually flash OTAs as soon as they are available you should keep in mind your setup is a bit different and instructions posted everywere won’t work exactly for your phone.&lt;/p&gt;

&lt;p&gt;To manually apply an OTA update, you should flash a custom recovery first, you can do it either from system using ‘&lt;tt&gt;dd&lt;/tt&gt;’ (you are root now), or through fastboot (but remember to oem-unlock your bootloader first using the BootUnlocker app, and re-lock when done).&lt;/p&gt;

&lt;p&gt;To flash the custom recovery from system, you can use the following command as root:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ su
# dd if=/sdcard/twrp-2.2.0-maguro.img of=/dev/block/platform/omap/omap_hsmmc.0/by-name/recovery
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can then reboot into the custom recovery to apply the OTA from sdcard, or to backup your system, remember to use a custom recovery with support for encrypted partitions like TWRP. Again, remember that when you boot your phone again, the custom recovery will be overwritten with the stock recovery on boot.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt; : An avid reader (thanks Marc!) noticed that factory images don’t include the files &lt;tt&gt;/system/etc/install-recovery.sh&lt;/tt&gt; and &lt;tt&gt;/system/recovery-from-boot.p&lt;/tt&gt; which are responsible of re-installing the factory image on boot when the installed recovery checksum doesn’t match the stock recovery, these files are only present if you have updated through an OTA. This means that if you come from a factory image, you’ll need to reflash the stock recovery manually at the end of the process. To do this first extract the stock recovery from a factory image:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ tar zxvf takju-factory.tgz
$ cd takju-*/
$ unzip image-takju-*.zip
[...]
  inflating: recovery.img
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then reboot into fastboot mode and flash it using fastboot: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fastboot flash recovery recovery.img&lt;/code&gt;&lt;/p&gt;</content><author><name></name></author><category term="android" /><category term="linux" /><category term="security" /><category term="android" /><category term="bootloader" /><category term="encryption" /><category term="fastboot" /><category term="galaxy nexus" /><category term="maguro" /><category term="root" /><category term="security" /><category term="superuser" /><summary type="html"></summary></entry><entry><title type="html">Ubuntu 12.04 on ONKYO BX407A4</title><link href="/2012/06/18/ubuntu-12-04-on-onkyo-bx407a4/" rel="alternate" type="text/html" title="Ubuntu 12.04 on ONKYO BX407A4" /><published>2012-06-18T19:27:44+00:00</published><updated>2012-06-18T19:27:44+00:00</updated><id>/2012/06/18/ubuntu-12-04-on-onkyo-bx407a4</id><content type="html" xml:base="/2012/06/18/ubuntu-12-04-on-onkyo-bx407a4/">&lt;p&gt;Two years ago I wrote about &lt;a href=&quot;/2010/07/30/linux-on-onkyo-bx407a4/&quot;&gt;installing Ubuntu 10.04 on ONKYO BX407A4&lt;/a&gt;, now I have upgraded it to 12.04 LTS, and here’s a reviewed procedure in case anyone still uses this UMPC.&lt;/p&gt;

&lt;p&gt;Basically everything works so well out of the box, except for a few things that need a little bit of tweaking:&lt;/p&gt;

&lt;p&gt;Replace &lt;tt&gt;/etc/rc.local&lt;/tt&gt; with the following: &lt;a href=&quot;/archives/files/ONKYO/rc.local&quot;&gt;rc.local&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Replace &lt;tt&gt;/etc/default/grub&lt;/tt&gt; with the following: &lt;a href=&quot;/archives/files/ONKYO/grub&quot;&gt;grub&lt;/a&gt;, when done run ‘&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo update-grub&lt;/code&gt;’.&lt;/p&gt;

&lt;p&gt;Add the file &lt;tt&gt;/etc/modprobe.d/blacklist-onkyo.conf&lt;/tt&gt;: &lt;a href=&quot;/archives/files/ONKYO/blacklist-onkyo.conf&quot;&gt;blacklist-onkyo.conf&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add the file &lt;tt&gt;/etc/pm/power.d/99_onkyo&lt;/tt&gt; and make it executable: &lt;a href=&quot;/archives/files/ONKYO/99_onkyo&quot;&gt;99_onkyo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add the file &lt;tt&gt;/etc/pm/sleep.d/74_onkyo&lt;/tt&gt; and make it executable: &lt;a href=&quot;/archives/files/ONKYO/74_onkyo&quot;&gt;74_onkyo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add the file &lt;tt&gt;/etc/pm/sleep.d/00_onkyo&lt;/tt&gt; and make it executable: &lt;a href=&quot;/archives/files/ONKYO/00_onkyo&quot;&gt;00_onkyo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add the file &lt;tt&gt;/etc/pm/config.d/onkyo_fix&lt;/tt&gt;: &lt;a href=&quot;/archives/files/ONKYO/onkyo_fix&quot;&gt;onkyo_fix&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Install vbetool:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo apt-get install vbetool
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Install the latest version of &lt;a href=&quot;https://github.com/poliva/opengalax&quot;&gt;opengalax Touch Screen Driver&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ sudo add-apt-repository ppa:poliva/opengalax
$ sudo apt-get update
$ sudo apt-get install opengalax xinput-calibrator
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;When done, edit the file /etc/opengalax.conf and set psmouse=1, this together with the changes in grub will make the touchscreen and the optical mouse work.&lt;/p&gt;

&lt;p&gt;Reboot, and run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xinput_calibrator&lt;/code&gt; to calibrate the screen, save the calibration data in &lt;tt&gt;/etc/X11/xorg.conf.d/99-calibration.conf&lt;/tt&gt;.&lt;/p&gt;

&lt;p&gt;Some other useful hints:&lt;/p&gt;

&lt;p&gt;Rember to add the ‘discard’ parameter to &lt;tt&gt;/etc/fstab&lt;/tt&gt; for SSD trim.&lt;/p&gt;

&lt;p&gt;For Ubuntu/Unity:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ sudo add-apt-repository ppa:poliva/pof
$ sudo apt-get update
$ sudo apt-get install indicator-sysbat
$ echo &quot;coretemp&quot; |sudo tee -a /etc/modules
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For Lubuntu/LXDE:&lt;br /&gt;
Install &lt;a href=&quot;https://github.com/poliva/lxbat&quot;&gt;lxbat&lt;/a&gt; instead.&lt;/p&gt;

&lt;p&gt;With all these changes everything works as expected, even suspend/resume :)&lt;/p&gt;</content><author><name></name></author><category term="gadgets" /><category term="linux" /><category term="minipost" /><category term="BX407A4" /><category term="egalax" /><category term="onkyo" /><category term="opengalax" /><summary type="html">Two years ago I wrote about installing Ubuntu 10.04 on ONKYO BX407A4, now I have upgraded it to 12.04 LTS, and here’s a reviewed procedure in case anyone still uses this UMPC.</summary></entry><entry><title type="html">Why Broadcom 802.11 Linux STA driver sucks, and how to fix it</title><link href="/2012/05/23/why-broadcom-80211-linux-sta-driver-sucks-and-how-to-fix-it/" rel="alternate" type="text/html" title="Why Broadcom 802.11 Linux STA driver sucks, and how to fix it" /><published>2012-05-22T22:49:17+00:00</published><updated>2012-05-22T22:49:17+00:00</updated><id>/2012/05/23/why-broadcom-80211-linux-sta-driver-sucks-and-how-to-fix-it</id><content type="html" xml:base="/2012/05/23/why-broadcom-80211-linux-sta-driver-sucks-and-how-to-fix-it/">&lt;blockquote&gt;TL;DR - the broadcom sta linux driver always fails in the first scan request after the interface is brought up, this produces a long delay when connecting to a wireless network. There's an open source driver which does not have this problem, but is not good with power management. In this post I describe the steps I took to pinpoint the problem in the proprietary driver and to fix it.&lt;/blockquote&gt;

&lt;p&gt;The story begins when I updated Ubuntu from 11.10 to 12.04 on my MacBook Air, everything worked fine after upgrading except one thing that bothered me a lot: when resuming the laptop after suspending it, it took around 30 seconds to connect to my wireless network. It wouldn’t have bothered me if it had been the same in 11.10, but in 11.10 the time to connect was barely 5 or 6 seconds, so having to wait 30 seconds was totally unacceptable.&lt;/p&gt;

&lt;p&gt;Initially I thought it was a bug in NetworkManager, and increased the debug level in the config file to finally come out to the conclusion that I was using a different driver in 12.04 than in 11.10.&lt;/p&gt;

&lt;p&gt;There are two drivers available for the Broadcom BCM4353 802.11 Wireless Controller:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a title=&quot;Broadcom brcmsmac&quot; href=&quot;http://linuxwireless.org/en/users/Drivers/brcm80211&quot;&gt;Broadcom brcmsmac (mac80211-based softmac PCIe)&lt;/a&gt;&lt;/strong&gt;: the completely open source drivers, included in the kernel&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a title=&quot;Broadcom 802.11 Linux STA driver&quot; href=&quot;http://www.broadcom.com/support/802.11/linux_sta.php&quot;&gt;Broadcom 802.11 Linux STA driver&lt;/a&gt;&lt;/strong&gt;: the broadcom mixed GPL source + a proprietary hybrid binary file agnostic to the specific version of the Linux kernel&lt;/li&gt;
&lt;/ul&gt;
&lt;!--more--&gt;
&lt;p&gt;Both wl (broadcom proprietary driver) and brcmsmac (the open source driver) were installed in my Ubuntu 11.10 but the open source driver was used by default, and this driver connected to the wifi network in 5 seconds.&lt;/p&gt;

&lt;p&gt;In Ubuntu 12.04, the wl proprietary driver provided by the package bcmwl-kernel-source has been updated from version 5.100.82.38+bdcom-0ubuntu4 to version 5.100.82.38+bdcom-0ubuntu6.1 which includes the following fix:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;---------------
bcmwl (5.100.82.38+bdcom-0ubuntu6.1) precise-proposed; urgency=low
	
  * debian/bcmwl-kernel-source.postinst:
    - Blacklist brcmfmac, brcmsmac and bcma so that they don't
      conflict with the closed driver (LP: #873117)
 -- Alberto Milone  Mon, 23 Apr 2012 16:11:56 +0200
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Which basically blacklists the open source brcmsmac module, forcing the wl proprietary driver to be in use. When the brcmsmac was not blacklisted, even if the wl driver was loaded it failed silently and brcmsmac was used instead.&lt;/p&gt;

&lt;p&gt;So, the easy path to solve my problem would have been to blacklist the wl module, and add the brcmsmac to &lt;tt&gt;/etc/modules&lt;/tt&gt; and live happy with my 5 seconds needed to associate when resuming, &lt;strong&gt;*BUT*&lt;/strong&gt; I compared both drivers and the proprietary driver has better signal and way better power management, which makes my battery last longer, so I decided to go the long route. My goal was to achieve the lowest delay possible to connect to a wireless network when coming from a suspend using the proprietary driver.&lt;/p&gt;

&lt;p&gt;I went “down” one level and started looking at wpasupplicant, as NetworkManager communicates with it using the DBus control interface (dbus-monitor showed the problem was not in dbus communication) so, increased the debug level in wpa-supplicant by adding ‘&lt;tt&gt;-dd&lt;/tt&gt;’ switch in &lt;tt&gt;/usr/share/dbus-1/system-services/fi.w1.wpa_supplicant1.service&lt;/tt&gt;, and looked through the logs, which quickly revealed the following:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;May 20 11:49:46 maco wpa_supplicant[12610]: Scan requested (ret=0) - scan timeout 5 seconds
May 20 11:49:52 maco wpa_supplicant[12610]: Scan timeout - try to get results
May 20 11:49:52 maco wpa_supplicant[12610]: Failed to get scan results
May 20 11:49:52 maco wpa_supplicant[12610]: Failed to get scan results - try scanning again
May 20 11:50:07 maco wpa_supplicant[12610]: Scan requested (ret=0) - scan timeout 5 seconds
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The first scan request (SIOCSIWSCAN), after the wireless interface was brought up always failed (?), and wpa_supplicant tried to get the scan results (SIOCGIWSCAN) because some drivers do not deliver SIOCGIWSCAN events to notify when scan is complete, but this failed too, so wpasupplicant requested a second scan after a timeout, which properly delivered the results this time. This first failing scan was adding 21 seconds of delay to the network association process.&lt;/p&gt;

&lt;p&gt;I googled the error and found I was not the only soul affected by this problem, Kalle Valo submitted 4 different patches to the hostap mailing list between October 2010 and March 2011, but the patches were never accepted upstream, nor included in the ubuntu package. The wpasupplicant code has changed a bit since Kalle submitted his patches, so I adapted them to the current wpa_supplicant version in Ubuntu. If you are curious, you can dig through &lt;a href=&quot;https://bugs.launchpad.net/bugs/994739&quot; title=&quot;wireless takes several seconds longer to connect from standby&quot;&gt;ubuntu bug #994739&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In short, the &lt;a href=&quot;http://lists.shmoo.com/pipermail/hostap/2011-March/022891.html&quot; title=&quot;Add a workaround for Broadcom wl driver's first failing scan&quot;&gt;version 4 patch&lt;/a&gt; from Kalle basically patches the WEXT driver from wpasupplicant to check the return value when trying to get scan results (SIOCGIWSCAN) from the wl driver, if the number of last error (errno) is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EINVAL&lt;/code&gt; on the first scan, it requests another scan, so this one will go through (as only the first one fails) and return the scan results next time wpasupplicant tries to get them. This is far from perfect, but it works (and doesn’t seem to break anything), reducing the time needed to associate to the wireless network after the interface has been brought up from 30 seconds to 12 seconds.&lt;/p&gt;

&lt;p&gt;But I was still unhappy with this result, so I &lt;a href=&quot;/archives/files/mba42/wpa-supplicant-fix-wl-driver.patch&quot; title=&quot;wpa supplicant fix wl driver patch&quot;&gt;patched&lt;/a&gt; the wpasupplicant code to request a scan right after the driver init function, so this would be the first “failing” scan, and the real scan requested a bit later would return results. This was a very ugly patch, because it made wpasupplicant request a scan in INACTIVE state (when it should be SCANNING), but it worked and reduced the time from 30 seconds to 10 seconds.&lt;/p&gt;

&lt;p&gt;So, still unhappy with the results, I decided to go down one more level and have a look at the GPL’d source of the Broadcom’s Linux STA proprietary driver, and BINGO! this is how the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wl_iw_set_scan()&lt;/code&gt; function ends:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;        (void) dev_wlc_ioctl(dev, WLC_SCAN, &amp;amp;ssid, sizeof(ssid));
	
        return 0;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;They always return 0, even when the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dev_wlc_ioctl()&lt;/code&gt; function fails!! and WTH is it casted to void?? It would have been easier to just return the result of this function!. Patching this shows that the first scan after the interface is up fails with errno &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBUSY&lt;/code&gt; (device or resource busy), so I added a workaround here to make it request the scan to the underlying hardware until it returned something different than &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBUSY&lt;/code&gt; and could be correctly handled by wpasupplicant, &lt;em&gt;et voilà&lt;/em&gt;, time reduced to 10 seconds.&lt;/p&gt;

&lt;p&gt;But hey, now that I looked at their source, it turns out that there’s a newer version available in broadcom’s website: 5.100.82.112. This version now supports the new linux cfg80211 wireless configuration API in addition to the older Wireless Extensions (WEXT), you can choose between CFG80211 or WEXT at compile time, the ubuntu package &lt;tt&gt;broadcom-sta-dkms&lt;/tt&gt; in the development release for 12.10 ‘Quantal Quetzal’ has been updated to this version but still uses the old WEXT which is still broken (always returns 0, remember above?). But, guess what they have done it correctly this time in the new CFG80211 code, see the end of the function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__wl_cfg80211_scan()&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;        err = wl_dev_ioctl(ndev, WLC_SCAN, &amp;amp;sr-&amp;gt;ssid, sizeof(sr-&amp;gt;ssid));
        if (err) {
                if (err == -EBUSY) {
                        WL_INF((\&quot;system busy : scan for \\&quot;%s\\&quot; \&quot;
                                \&quot;canceled\n\&quot;, sr-&amp;gt;ssid.SSID));
                } else {
                        WL_ERR((\&quot;WLC_SCAN error (%d)\n\&quot;, err));
                }
                goto scan_out;
        }
	
        return 0;
	
scan_out:
        clear_bit(WL_STATUS_SCANNING, &amp;amp;wl-&amp;gt;status);
        wl-&amp;gt;scan_request = NULL;
        return err;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As you can see, they now return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EBUSY&lt;/code&gt; when the driver cannot perform the scan, and wpa_supplicant can manage this situation correctly, so I quickly backported the &lt;tt&gt;broadcom-sta-dkms&lt;/tt&gt; package from ubuntu 12.10 to ubuntu 12.04 and added a patch to compile it with CFG80211 enabled, and finally &lt;strong&gt;I CAN HAS ONLY 8 SECONDS DELAY!!!!1&lt;/strong&gt; to associate to the wifi network after my laptop resumes from suspend using the wl driver, and I’m a happy camper! :D&lt;/p&gt;</content><author><name></name></author><category term="linux" /><category term="wireless" /><category term="brcmsmac" /><category term="broadcom" /><category term="CFG80211" /><category term="dkms" /><category term="kernel" /><category term="lkm" /><category term="module" /><category term="networkmanager" /><category term="ubuntu" /><category term="WEXT" /><category term="wifi" /><category term="wireless" /><category term="wl" /><category term="wpasupplicant" /><summary type="html">TL;DR - the broadcom sta linux driver always fails in the first scan request after the interface is brought up, this produces a long delay when connecting to a wireless network. There's an open source driver which does not have this problem, but is not good with power management. In this post I describe the steps I took to pinpoint the problem in the proprietary driver and to fix it.</summary></entry><entry><title type="html">How to have your patch included in Ubuntu</title><link href="/2012/05/21/have-a-patch-included-in-ubuntu/" rel="alternate" type="text/html" title="How to have your patch included in Ubuntu" /><published>2012-05-21T20:56:05+00:00</published><updated>2012-05-21T20:56:05+00:00</updated><id>/2012/05/21/have-a-patch-included-in-ubuntu</id><content type="html" xml:base="/2012/05/21/have-a-patch-included-in-ubuntu/">&lt;p&gt;Today I submitted a patch to wpa-supplicant package in Ubuntu (&lt;a href=&quot;https://bugs.launchpad.net/bugs/994739&quot;&gt;LP: #994739&lt;/a&gt;) and I learnt the process of pushing your changes to Launchpad and ask for them to be reviewed and merged. Ubuntu has a great documentation on how to fix bugs &lt;a href=&quot;https://wiki.ubuntu.com/Bugs/HowToFix&quot;&gt;in this Wiki&lt;/a&gt; and &lt;a href=&quot;http://developer.ubuntu.com/packaging/html/fixing-a-bug.html&quot;&gt;packaging guide&lt;/a&gt;, just for future reference here are the steps I followed.&lt;/p&gt;

&lt;p&gt;If you don’t have them, get the ubuntu-dev-tools, and bazar VCS:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ sudo apt-get install bzr ubuntu-dev-tools $ bzr whoami &quot;Your Name \&amp;lt;you@example.org\&amp;gt;&quot; $ bzr launchpad-login poliva
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then get the source of the package you want to fix, and apply your patch:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ bzr branch lp:ubuntu/precise/wpasupplicant $ cd wpasupplicant $ patch -p1 \&amp;lt; /path/to/your.patch
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Prepare to build the ubuntu package:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ dpkg-source --commit $ dpkg-buildpackage -us -uc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In case &lt;tt&gt;dpkg-buildpackage&lt;/tt&gt; complains about no upstream tarball found, you can obtain the .orig.tar.gz file with &lt;tt&gt;apt-get source&lt;/tt&gt; and copy it to the base folder of your work.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ apt-get source wpasupplicant
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Finally, update the debian changelog and commit the change locally:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ dch -i $ bzr commit
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If all looks good, you can push the fix to launchpad and propose merging it into the official package:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;bzr push lp:~\&amp;lt;your-launchpad-id\&amp;gt;/ubuntu/\&amp;lt;release\&amp;gt;/\&amp;lt;package\&amp;gt;/\&amp;lt;branchname\&amp;gt; bzr lp-open
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The last command will open the Launchpad page of the remote branch in your browser. There click on the “(+) Propose for merging” link, to get the change reviewed by somebody and included in Ubuntu.&lt;/p&gt;</content><author><name></name></author><category term="linux" /><category term="minipost" /><category term="deb" /><category term="debian" /><category term="fix" /><category term="package" /><category term="packaging" /><category term="patch" /><category term="ubuntu" /><category term="wpasupplicant" /><summary type="html">Today I submitted a patch to wpa-supplicant package in Ubuntu (LP: #994739) and I learnt the process of pushing your changes to Launchpad and ask for them to be reviewed and merged. Ubuntu has a great documentation on how to fix bugs in this Wiki and packaging guide, just for future reference here are the steps I followed.</summary></entry><entry><title type="html">Getting started on Android Development from Command Line</title><link href="/2012/01/25/android-development-from-command-line/" rel="alternate" type="text/html" title="Getting started on Android Development from Command Line" /><published>2012-01-25T11:01:14+00:00</published><updated>2012-01-25T11:01:14+00:00</updated><id>/2012/01/25/android-development-from-command-line</id><content type="html" xml:base="/2012/01/25/android-development-from-command-line/">&lt;p&gt;As a quick reference, here’s a list of useful and most commonly used commands if you want to do Android development from command line (for example, without using eclipse or any other bloated IDE). You must have installed the &lt;a href=&quot;http://developer.android.com/sdk/index.html&quot;&gt;Android SDK&lt;/a&gt; on your system, and make sure the &lt;tt&gt;tools&lt;/tt&gt; and &lt;tt&gt;platform-tools&lt;/tt&gt; folders from the android SDK are available in your &lt;tt&gt;PATH&lt;/tt&gt; environment variable:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ANDROID_SDK=&quot;/home/user/android-sdk-linux_x86&quot;
export PATH=&quot;${PATH}:${ANDROID_SDK}/tools:${ANDROID_SDK}/platform-tools&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;!--more--&gt;
&lt;p&gt;If your SDK installation is not up to date, you can update it using the &lt;code&gt;android update sdk&lt;/code&gt; command. To generate a list of system image targets use &lt;code&gt;android list targets&lt;/code&gt;, this will produce output similar to this (stripped down for better readability):&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;id: 1 or &quot;android-3&quot;
     Name: Android 1.5
     API level: 3
id: 3 or &quot;android-4&quot;
     Name: Android 1.6
     API level: 4
id: 5 or &quot;android-7&quot;
     Name: Android 2.1-update1
     API level: 7
id: 7 or &quot;android-8&quot;
     Name: Android 2.2
     API level: 8
id: 10 or &quot;android-9&quot;
     Name: Android 2.3.1
     API level: 9
id: 12 or &quot;android-10&quot;
     Name: Android 2.3.3
     API level: 10
id: 14 or &quot;android-11&quot;
     Name: Android 3.0
     API level: 11
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;First thing you want to do is create an AVD (Android Virtual Device), this will create an emulator image where you can test your apps without using a real device, the android version used is specified with the &lt;code&gt;-t&lt;/code&gt; command switch, and must be one of the targets available in the command we used avobe.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;android create avd -n &amp;lt;name&amp;gt; -t &amp;lt;targetID&amp;gt; [-&amp;lt;option&amp;gt; &amp;lt;value&amp;gt;] ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;For example, to create an AVD running android 2.3.3, you would use the command &lt;code&gt;android create avd -n Android233 -t 12&lt;/code&gt;.
To list all existing AVDs, you can use the command &lt;code&gt;android list avd&lt;/code&gt;, this will produce output similar to this:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Available Android Virtual Devices:
    Name: Android233
  Target: Android 2.3.3 (API level 10)
    Skin: WVGA800
  Sdcard: 15M
---------
    Name: Android16
  Target: Android 1.6 (API level 4)
    Skin: WVGA800
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To delete an AVD we no longer need, we can use the command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;android delete avd -n &amp;lt;name&amp;gt;&lt;/code&gt;.&lt;br /&gt;
To launch the AVD, just use the command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;emulator -avd &amp;lt;name&amp;gt;&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2012/01/emulator16.png&quot; alt=&quot;android emulator - AVD&quot; title=&quot;Android Virtual Device&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now it’s time to create our first project, we will use the command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;android create project&lt;/code&gt; with the following command line parameters:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;android create project --target 1 --name MyApp --path ./MyProject --activity MyActivity --package com.example.myapp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Target&lt;/code&gt; specifies the minimum android version our project will be able to run, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;name&lt;/code&gt; is just the name of the application, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;path&lt;/code&gt; specifies the path in your hard drive where the project will be stored, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;activity&lt;/code&gt; specifies the name of the first &lt;tt&gt;Activity&lt;/tt&gt; that will be run when we launch the program (an &lt;a href=&quot;http://developer.android.com/guide/topics/fundamentals/activities.html&quot;&gt;Activity&lt;/a&gt; is an application component that provides a screen with which users can interact in order to do something), and finally &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;package&lt;/code&gt; specifies the namespace that will be used (it must be a unique package name across all packages installed on the Android system, following the same rules as for packages in Java).&lt;/p&gt;

&lt;p&gt;Now it’s time to fire up your favorite text editor (vim FTW!!), and start coding… if you don’t know where to start, a good starting point is reading the &lt;a href=&quot;http://developer.android.com/guide/topics/fundamentals.html&quot;&gt;Android Application Fundamentals&lt;/a&gt; from the Android Developers guide.&lt;/p&gt;

&lt;p&gt;If you’re upgrading a project from an older version of the Android SDK or want to create a new project from existing code, we will use the command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;android update project&lt;/code&gt; as folows:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;android update project --name MyApp --target 2 --path ./MyProject
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The following commands are useful when you have finished coding your application and want to compile, run or debug it:&lt;/p&gt;

&lt;p&gt;Building in debug mode:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cd ./MyProject &amp;amp;&amp;amp; ant debug
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Building in debug mode and install on already running emulator:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cd ./MyProject &amp;amp;&amp;amp; ant install
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Automatically launch app on the running emulator after building it:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;adb shell 'am start -n com.example.myapp/.MyActivity'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And now, all together: build in debug mode, install and run on emulator:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cd ./MyProject &amp;amp;&amp;amp; ant install &amp;amp;&amp;amp; adb shell 'am start -n com.example.myapp/.MyActivity'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;When you are happy with the results, you might want to build your app in release mode, for example to publish it on the android market:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cd ./MyProject &amp;amp;&amp;amp; ant release
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note that output needs to be signed and zipaligned before it can be uploaded on the &lt;a href=&quot;https://market.android.com/&quot;&gt;Android Market&lt;/a&gt;, you might want to read the &lt;a href=&quot;http://developer.android.com/guide/publishing/app-signing.html&quot;&gt;Signing Your Applications&lt;/a&gt; chapter in the Development guide for instructions on how to generate your private key for signing your applications.&lt;/p&gt;

&lt;p&gt;If you want to build in release mode, with the output signed and aligned, you can do it by modifying the &lt;tt&gt;ant.properties&lt;/tt&gt; file in your project as follows:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;echo &quot;key.store=path/to/my.keystore&quot; &amp;gt;&amp;gt; ./MyProject/ant.properties
echo &quot;key.alias=mykeystore&quot; &amp;gt;&amp;gt; ./MyProject/ant.properties
cd ./MyProject &amp;amp;&amp;amp; ant release
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Finally, to wrap up everything we will create a hello world app from command line:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;android create project --package com.example.helloandroid --activity HelloAndroid --target 2 --path ./HelloAndroid
	
tee ./HelloAndroid/src/com/example/helloandroid/HelloAndroid.java &amp;lt;&amp;lt;-EOF
package com.example.helloandroid;
	
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
	
public class HelloAndroid extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        TextView tv = new TextView(this);
        tv.setText(&quot;Hello, Android&quot;);
        setContentView(tv);
    }
}
EOF
android list avd
emulator -avd XXXXX &amp;amp;
cd ./MyProject
adb devices
ant install
adb shell 'am start -n com.example.helloandroid/.HelloAndroid'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Happy coding! :D&lt;/p&gt;</content><author><name></name></author><category term="android" /><category term="android" /><category term="apk" /><category term="application" /><category term="command" /><category term="commandline" /><category term="development" /><category term="eclipse" /><category term="hello" /><category term="hello world" /><category term="java" /><category term="line" /><category term="shell" /><category term="world" /><summary type="html">As a quick reference, here’s a list of useful and most commonly used commands if you want to do Android development from command line (for example, without using eclipse or any other bloated IDE). You must have installed the Android SDK on your system, and make sure the tools and platform-tools folders from the android SDK are available in your PATH environment variable:</summary></entry></feed>