Generalised boundary terms for higher derivative theories of gravity