Thursday, October 27, 2011

OSX Lion Boot Options

This is a reminder for myself~

Once Mac OSX Lion starts booting up, you may press the following key combinations for analyzing and/or fixing your system:
  • Command + V => Verbose mode, so you can check what makes your system wait or which error makes it stuck.
  • Command + R => Recovery mode, you can access disk utilities to check and/or fix your disk, access the Internet for solution, and reinstall your OSX.
  • Option => You can select which disk to boot
  • Command + S => Single user mode, the system will boot into terminal mode and login as root. You can also try to check and fix your disk here, via commands (fsck, fsck_hfs etc.)
  • C => Boot CD/DVD rom.

Thursday, August 11, 2011

Programming with Windows Screensaver

While developing your Windows application/system, you may want to disable/enable/deactivate the screensaver. Here are some ways to do so: (ref:

If the screensaver is activated (because halting for some time) and you want to kill it (activate the machine), you may use the following C code:

To temporally disable screensaver (maybe your application is working on something and you do not want the screen to turn off automatically), you may use:

However, your system may go wrong using the above code. Imagine if your application crashed after disabling the screensaver, then it will keep disabled until you re-enable it manually. In this case, you can just set the state of your current thread to tell the system that this thread requires attention.

Monday, July 4, 2011

Android Kill Self-process

I came across a line of source code written by some others today, and it gives me a behavior I have never thought about. The whole application (simplified version) consists of two activities. A user first launches up the main activity and clicks a button in this activity. This starts the second activity. The user then clicks a button in the second activity, and this line is called:

I thought the whole application will be killed and the system will go back to home screen but it does not. Instead the application goes back to main activity and calls its onCreate() again. I am surprised, and then I did a test. I added finish() in main activity after startActivity(). This time, killing the process just closed the whole application.

I tried to search for the reason of this behavior but I cannot find out the answer. Normally, processes are managed by Android system and one should not handle them himself. I believe this behavior is because some resource is not freed yet when the process is killed, and the system thinks the application should relaunch (or it thinks the application is still launching for the first time?).

Sunday, May 29, 2011

Some Notes about GINA

GINA (Graphical Identification and Authentication) is a component which works with Winlogon to provide secure authentication and interactive logon services for Windows before Windows Vista. GINA is implemented as a replaceable dynamic link library (DLL), so that one can customize it by writing his own DLL. For GINA basics, please read them here. Below I would like to make some notes about problems I met while developing a customized GINA. I will expand the list when I find something new.
  1. Do not call WlxSasNotify() to notify Winlogon for a SAS (Secure Attention Sequence, normally Ctrl+Alt+Del) in a thread different from the GINA thread. In my case, I post a message using PostMessage() SendMessage() to the SAS dialog box and call WlxSasNotify() there. Otherwise, the machine failed to shut down after recovering from hibernate.
  2. Normally, Winlogon will capture a SAS appearance and call appropriate GINA functions. If you are replacing it with something else (so you are not triggering SAS manually), I have found that Winlogon may fail to call those GINA functions and results in halting between logon and logoff/locked states. Therefore you should notify Winlogon using WlxSasNotify(). However, you cannot just call WlxSasNotify() in anywhere, otherwise you may trigger some other abnormal behavior. After trial and error, I found that only calling WlxSasNotify() from the dialog box gives out normal results.
Update: Using PostMessage() will trigger another bug (cannot shut down after recovering from standby)

Sunday, April 3, 2011

Static Class Member Variables in C++

Recently when I tried to write a class with static member functions and variables, I found a strange linking error. No matter how I tried, the linker did not seem to be able to link those static member variables properly. After some searching, it seems to be a common compiler "feature" of C++....

In order to define the variable, define it outside the class definition, but the declaration is still inside the class.

Friday, March 18, 2011

Observations of MIFARE Classic 4K Card

Recently I am developing an application for Windows environment with smartcard, and below are some notes I would like to make about MIFARE Classic 4K card:

  • Block 0 is not writable. It contains card ID and information stored by manufacturer
  • Blocks are divided into sectors, and each sector contains one trailer block
  • Trailer block stored keys to authenticate the sector it belongs and attributes which decide access right to the sector
  • Handling the trailer block not carefully may lead to all rights to that sector disabled, and I have successfully (or I should say accidentally...) locked the whole sector 0. However, as the GetID command is still functional, that command should be using some other way to read the ID from block 0 / some hidden block?
  • As trailer block stores the authentication keys of its sector, updating them means changing the authentication keys. Location of keys: first 6 bytes -> Type A; last 6 bytes: Type B
  • Reading/Writing a data block requires authentication beforehand
  • Authenticating one block is equivalent to authenticate the whole sector
  • Only one sector can be authenticated at any time. E.g. If you authenticate sector 2 after authenticating sector 1, you can now only access sector 2 but not sector 1. In order to access sector 1, you have to authenticate sector 1 again.
  • The above does not only apply to one single application. i.e. If application A authenticated sector 1 and then application B authenticate sector 2, application A will not be able to access sector 1 without authenticating sector 1 again.

Monday, February 28, 2011

Using sysinfo in Android NDK

Sometimes you may need to write a program which needs to query some system information (e.g. system uptime). In this case, one may want to call the sysinfo() with struct sysinfo as defined in <sys/sysinfo.h>. Unfortunately, Android NDK does not provide the sysinfo() definition (definition is included since android-9, but I am still having no luck to link it properly). After some searching, I found this thread provides a solution: adding an assembly source (.S). Build it with your project and it works like a charm. The assembly source is attached below for your reference.

Saturday, February 26, 2011

STLport in Android NDK r5b

Android NDK includes a special version of STLport since r5, and let developers to use it by specifying a flag in files. However, if you are developing using Makefile instead (like I do), using STLport may be a little bit complicated.

First, since the build-standalone-toolchain script of NDK (also included since r5) can only include stdc++ library into the toolchain it builds, compiling with STLport headers may lead to a list of build errors. In order to prevent this, using prebuilt compilers in the NDK ("the hard way" as described in readme) may be a better choice. In this case, you will have to take care with sysroot.

Second, developing with this special STLport has some limitations as mentioned in readme: Exception and RTTI are not supported. Remember to build every cpp source files with -fno-exceptions and -fno-rtti.